First working test version of oob support in the websocket client.

This commit is contained in:
Griatch 2014-06-25 23:35:21 +02:00
parent a9cf081494
commit ca1e36da5f
5 changed files with 48 additions and 26 deletions

View file

@ -198,7 +198,7 @@ class ReportAttributeTracker(TrackerBase):
# eventual args/kwargs. All functions defined globally in this
# module will be made available to call by the oobhandler. Use
# _funcname if you want to exclude one. To allow for python-names
# like "list" here, these properties are read as being case-insensitive.
# like "list" here, these properties are case-insensitive.
#
# All OOB commands must be on the form
# cmdname(oobhandler, session, *args, **kwargs)
@ -212,7 +212,7 @@ def oob_error(oobhandler, session, errmsg, *args, **kwargs):
"""
session.msg(oob=("send", {"ERROR": errmsg}))
def list(oobhandler, session, mode, *args, **kwargs):
def LIST(oobhandler, session, mode, *args, **kwargs):
"""
List available properties. Mode is the type of information
desired:
@ -260,7 +260,7 @@ def list(oobhandler, session, mode, *args, **kwargs):
session.msg(oob=("list", ("unsupported mode",)))
def send(oobhandler, session, *args, **kwargs):
def SEND(oobhandler, session, *args, **kwargs):
"""
This function directly returns the value of the given variable to the
session. vartype can be one of
@ -278,7 +278,7 @@ def send(oobhandler, session, *args, **kwargs):
session.msg(oob=("send", ret))
def report(oobhandler, session, *args, **kwargs):
def REPORT(oobhandler, session, *args, **kwargs):
"""
This creates a tracker instance to track the data given in *args.
vartype is one of "prop" (database fields) or "attr" (attributes)
@ -296,7 +296,7 @@ def report(oobhandler, session, *args, **kwargs):
key, ReportAttributeTracker)
def unreport(oobhandler, session, vartype="prop", *args, **kwargs):
def UNREPORT(oobhandler, session, vartype="prop", *args, **kwargs):
"""
This removes tracking for the given data given in *args.
vartype is one of of "prop" or "attr".
@ -311,6 +311,7 @@ def unreport(oobhandler, session, vartype="prop", *args, **kwargs):
else: # assume attribute
oobhandler.untrack_attribute(obj, session.sessid, key)
def echo(oobhandler, session, *args, **kwargs):
def ECHO(oobhandler, session, *args, **kwargs):
"Test function, returning the args, kwargs"
session.msg(oob=("send", args, kwargs))
args = ["Return echo:"] + list(args)
session.msg(oob=("echo", args, kwargs))

View file

@ -347,7 +347,7 @@ class OOBHandler(object):
using *args and **kwargs
"""
try:
#print "OOB execute_cmd:", session, func_key, args, kwargs, _OOB_FUNCS.keys()
print "OOB execute_cmd:", session, func_key, args, kwargs, _OOB_FUNCS.keys()
oobfunc = _OOB_FUNCS[func_key] # raise traceback if not found
oobfunc(self, session, *args, **kwargs)
except KeyError,e:

View file

@ -73,7 +73,7 @@ class WebSocketProtocol(Protocol, Session):
prefix.
OOB - This is an Out-of-band instruction. If so,
the remaining string should be a json-packed
string on the form {oobfuncname: [[args], {kwargs}], ...}
string on the form {oobfuncname: [args, ], ...}
any other prefix (or lack of prefix) is considered
plain text data, to be treated like a game
input command.
@ -82,10 +82,8 @@ class WebSocketProtocol(Protocol, Session):
string = string[3:]
try:
oobdata = json.loads(string)
for (key, argstuple) in oobdata.items():
args = argstuple[0] if argstuple else []
kwargs = argstuple[1] if len(argstuple) > 1 else {}
self.data_in(text=None, oob=(key, args, kwargs))
for (key, args) in oobdata.items():
self.data_in(text=None, oob=(key, args))
except Exception:
log_trace("Websocket malformed OOB request: %s" % string)
else:

View file

@ -104,7 +104,14 @@ class SessionHandler(object):
"""
Helper method for each session to use to parse oob structures
(The 'oob' kwarg of the msg() method)
((cmdname, (args), {}), ...)
Allowed oob structures are:
allowed oob structures are
cmdname
((cmdname,), (cmdname,))
(cmdname,(arg, ))
@ -134,10 +141,10 @@ class SessionHandler(object):
return (oobstruct[0].lower(), (), dict(oobstruct[1]))
elif isinstance(oobstruct[1], (tuple, list)):
# cmdname, (args,)
return (oobstruct[0].lower(), tuple(oobstruct[1]), {})
return (oobstruct[0].lower(), list(oobstruct[1]), {})
else:
# cmdname, (args,), {kwargs}
return (oobstruct[0].lower(), tuple(oobstruct[1]), dict(oobstruct[2]))
return (oobstruct[0].lower(), list(oobstruct[1]), dict(oobstruct[2]))
if hasattr(oobstruct, "__iter__"):
# differentiate between (cmdname, cmdname),
@ -145,12 +152,12 @@ class SessionHandler(object):
# (cmdname,args,kwargs), ...)
if oobstruct and isinstance(oobstruct[0], basestring):
return (tuple(_parse(oobstruct)),)
return (list(_parse(oobstruct)),)
else:
out = []
for oobpart in oobstruct:
out.append(_parse(oobpart))
return (tuple(out),)
return (list(out),)
return (_parse(oobstruct),)

View file

@ -25,6 +25,7 @@ messages sent to the client is one of three modes:
var wsurl = "ws://localhost:8001";
function webclient_init(){
// initializing the client once the html page has loaded
websocket = new WebSocket(wsurl);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
@ -33,6 +34,7 @@ function webclient_init(){
}
function onOpen(evt) {
// client is just connecting
$("#connecting").remove(); // remove the "connecting ..." message
msg_display("sys", "Using websockets - connected to " + wsurl + ".")
@ -42,24 +44,25 @@ function onOpen(evt) {
}
function onClose(evt) {
// client is closing
CLIENT_HASH = 0;
alert("Mud client connection was closed cleanly.");
}
function onMessage(evt) {
// outgoing message from server
var inmsg = evt.data
if (inmsg.length > 3 && inmsg.substr(0, 3) == "OOB") {
// dynamically call oob methods, if available
try {var oobtuples = JSON.parse(inmsg.slice(4));} // everything after OOB }
try {var oobarray = JSON.parse(inmsg.slice(3));} // everything after OOB }
catch(err) {
// not JSON packed - a normal text
msg_display('out', inmsg);
msg_display('out', err + " " + inmsg);
return;
}
for (var oobtuple in oobtuples) {
try { window[oobtuple[0]](oobtuple[1]) }
catch(err) { msg_display("err", "Could not execute OOB function " + oobtuple[0] + "!") }
for (var ind in oobarray) {
try { window[oobarray[ind][0]](oobarray[ind][1]) }
catch(err) { msg_display("err", "Could not execute OOB function " + oobtuple[0] + "(" + oobtuple[1] + ")!") }
}
}
else {
@ -68,10 +71,12 @@ function onMessage(evt) {
}
function onError(evt) {
// client error message
msg_display('err', "Error: Server returned an error. Try reloading the page.");
}
function doSend(){
// sending data from client to server
outmsg = $("#inputfield").val();
history_add(outmsg);
HISTORY_POS = 0;
@ -84,12 +89,23 @@ function doSend(){
websocket.send(outmsg); }
}
function doOOB(ooblist){
// Takes an array on form [funcname, [args], funcname, [args], ... ]
var oobmsg = JSON.stringify(ooblist);
function doOOB(oobdict){
// Handle OOB communication from client side
// Takes a dict on form {funcname:[args], funcname: [args], ... ]
msg_display("out", "into doOOB: " + oobdict)
msg_display("out", "stringify: " + JSON.stringify(oobdict))
var oobmsg = JSON.stringify(oobdict);
websocket.send("OOB" + oobmsg);
}
//
// OOB functions
//
function echo(message) {
msg_display("out", "ECHO return: " + message) }
//
// Display messages