Made ajax/comet client fallback work correctly in the new framework.

This commit is contained in:
Griatch 2016-02-14 13:29:41 +01:00
parent 83570848d6
commit 166189a7a5
7 changed files with 168 additions and 108 deletions

View file

@ -93,7 +93,7 @@ class WebSocketClient(Protocol, Session):
cmdarray = json.loads(string)
print "dataReceived:", cmdarray
if cmdarray:
self.data_in(**{cmdarray[0]:[cmdarray[1], cmdarray[2]]})
self.data_in(**{cmdarray[0], [cmdarray[1], cmdarray[2]]})
def sendLine(self, line):
"""
@ -147,6 +147,8 @@ class WebSocketClient(Protocol, Session):
text = args[0]
if text is None:
return
text = to_str(text, force_string=True)
options = kwargs.get("options", {})
raw = options.get("raw", False)
nomarkup = options.get("nomarkup", False)
@ -186,5 +188,5 @@ class WebSocketClient(Protocol, Session):
"""
if not cmdname == "options":
print "send_default", cmdname, args, kwargs
print "websocket.send_default", cmdname, args, kwargs
session.sendLine(json.dumps([cmdname, args, kwargs]))

View file

@ -1,11 +1,11 @@
"""
AJAX fallback webclient
AJAX/COMET fallback webclient
The AJAX web client consists of two components running
on twisted and django. They are both a part of the Evennia
website url tree (so the testing website might be located
on http://localhost:8000/, whereas the webclient can be
found on http://localhost:8000/webclient.)
The AJAX/COMET web client consists of two components running on
twisted and django. They are both a part of the Evennia website url
tree (so the testing website might be located on
http://localhost:8000/, whereas the webclient can be found on
http://localhost:8000/webclient.)
/webclient - this url is handled through django's template
system and serves the html page for the client
@ -79,30 +79,26 @@ class WebClient(resource.Resource):
except KeyError:
pass
def lineSend(self, suid, string, data=None):
def lineSend(self, suid, data):
"""
This adds the data to the buffer and/or sends it to the client
as soon as possible.
Args:
suid (int): Session id.
string (str): The text to send.
data (dict): Optional data.
Notes:
The `data` keyword is deprecated.
data (list): A send structure [cmdname, [args], {kwargs}].
"""
request = self.requests.get(suid)
if request:
# we have a request waiting. Return immediately.
request.write(jsonify({'msg': string, 'data': data}))
request.write(jsonify(data))
request.finish()
del self.requests[suid]
else:
# no waiting request. Store data in buffer
dataentries = self.databuffer.get(suid, [])
dataentries.append(jsonify({'msg': string, 'data': data}))
dataentries.append(jsonify(data))
self.databuffer[suid] = dataentries
def client_disconnect(self, suid):
@ -128,9 +124,6 @@ class WebClient(resource.Resource):
request (Request): Incoming request.
"""
#csess = request.getSession() # obs, this is a cookie, not
# an evennia session!
#csees.expireCallbacks.append(lambda : )
suid = request.args.get('suid', ['0'])[0]
remote_addr = request.getClientIP()
@ -158,14 +151,13 @@ class WebClient(resource.Resource):
"""
suid = request.args.get('suid', ['0'])[0]
if suid == '0':
return ''
return '""'
sess = self.sessionhandler.session_from_suid(suid)
if sess:
sess = sess[0]
text = request.args.get('msg', [''])[0]
data = request.args.get('data', [None])[0]
sess.sessionhandler.data_in(sess, text, data=data)
return ''
cmdarray = json.loads(request.args.get('data')[0])
sess.sessionhandler.data_in(sess, **{cmdarray[0]:[cmdarray[1], cmdarray[2]]})
return '""'
def mode_receive(self, request):
"""
@ -180,7 +172,7 @@ class WebClient(resource.Resource):
"""
suid = request.args.get('suid', ['0'])[0]
if suid == '0':
return ''
return '""'
dataentries = self.databuffer.get(suid, [])
if dataentries:
@ -210,7 +202,7 @@ class WebClient(resource.Resource):
except IndexError:
self.client_disconnect(suid)
pass
return ''
return '""'
def render_POST(self, request):
"""
@ -240,7 +232,7 @@ class WebClient(resource.Resource):
return self.mode_close(request)
else:
# this should not happen if client sends valid data.
return ''
return '""'
#
@ -261,28 +253,63 @@ class WebClientSession(session.Session):
reason (str): Motivation for the disconnect.
"""
if reason:
self.client.lineSend(self.suid, reason)
self.client.lineSend(self.suid, ["text", [reason], {}])
self.client.client_disconnect(self.suid)
def data_out(self, text=None, **kwargs):
def data_out(self, **kwargs):
"""
Data Evennia -> User access hook.
Data Evennia -> User
Kwargs:
kwargs (any): Options to the protocol
"""
self.sessionhandler.data_out(self, **kwargs)
def send_text(self, *args, **kwargs):
"""
Send text data.
Args:
text (str): The first argument is always the text string to send. No other arguments
are considered.
Kwargs:
raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
nomarkup (bool): Clean out all ansi/html markers and tokens.
"""
# string handling is similar to telnet
try:
text = utils.to_str(text if text else "", encoding=self.encoding)
raw = kwargs.get("raw", False)
nomarkup = kwargs.get("nomarkup", False)
if raw:
self.client.lineSend(self.suid, text)
else:
self.client.lineSend(self.suid,
parse_html(text, strip_ansi=nomarkup))
return
except Exception:
logger.log_trace()
if args:
args = list(args)
text = args[0]
if text is None:
return
text = utils.to_str(text, force_string=True)
options = kwargs.get("options", {})
raw = options.get("raw", False)
nomarkup = options.get("nomarkup", False)
if raw:
args[0] = text
else:
args[0] = parse_html(text, strip_ansi=nomarkup)
self.client.lineSend(self.suid, ["text", args, kwargs])
def send_default(self, cmdname, *args, **kwargs):
"""
Data Evennia -> User.
Args:
cmdname (str): The first argument will always be the oob cmd name.
*args (any): Remaining args will be arguments for `cmd`.
Kwargs:
options (dict): These are ignored for oob commands. Use command
arguments (which can hold dicts) to send instructions to the
client instead.
"""
if not cmdname == "options":
print "ajax.send_default", cmdname, args, kwargs
self.client.lineSend(self.suid, [cmdname, args, kwargs])

View file

@ -276,8 +276,8 @@ class Evennia(object):
with open(SERVER_RESTART, 'r') as f:
mode = f.read()
if mode in ('True', 'reload'):
from evennia.server.oobhandler import OOB_HANDLER
OOB_HANDLER.restore()
from evennia.scripts.monitorhandler import MONITOR_HANDLER
MONITOR_HANDLER.restore()
from evennia.scripts.tickerhandler import TICKER_HANDLER
TICKER_HANDLER.restore()
@ -352,9 +352,9 @@ class Evennia(object):
yield [(s.pause(manual_pause=False), s.at_server_reload()) for s in ScriptDB.get_all_cached_instances() if s.is_active]
yield self.sessions.all_sessions_portal_sync()
self.at_server_reload_stop()
# only save OOB state on reload, not on shutdown/reset
from evennia.server.oobhandler import OOB_HANDLER
OOB_HANDLER.save()
# only save monitor state on reload, not on shutdown/reset
from evennia.scripts.monitorhandler import MONITOR_HANDLER
MONITOR_HANDLER.save()
else:
if mode == 'reset':
# like shutdown but don't unset the is_connected flag and don't disconnect sessions