First working test version of oob support in the websocket client.
This commit is contained in:
parent
a9cf081494
commit
ca1e36da5f
5 changed files with 48 additions and 26 deletions
|
|
@ -198,7 +198,7 @@ class ReportAttributeTracker(TrackerBase):
|
||||||
# eventual args/kwargs. All functions defined globally in this
|
# eventual args/kwargs. All functions defined globally in this
|
||||||
# module will be made available to call by the oobhandler. Use
|
# module will be made available to call by the oobhandler. Use
|
||||||
# _funcname if you want to exclude one. To allow for python-names
|
# _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
|
# All OOB commands must be on the form
|
||||||
# cmdname(oobhandler, session, *args, **kwargs)
|
# cmdname(oobhandler, session, *args, **kwargs)
|
||||||
|
|
@ -212,7 +212,7 @@ def oob_error(oobhandler, session, errmsg, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
session.msg(oob=("send", {"ERROR": errmsg}))
|
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
|
List available properties. Mode is the type of information
|
||||||
desired:
|
desired:
|
||||||
|
|
@ -260,7 +260,7 @@ def list(oobhandler, session, mode, *args, **kwargs):
|
||||||
session.msg(oob=("list", ("unsupported mode",)))
|
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
|
This function directly returns the value of the given variable to the
|
||||||
session. vartype can be one of
|
session. vartype can be one of
|
||||||
|
|
@ -278,7 +278,7 @@ def send(oobhandler, session, *args, **kwargs):
|
||||||
session.msg(oob=("send", ret))
|
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.
|
This creates a tracker instance to track the data given in *args.
|
||||||
vartype is one of "prop" (database fields) or "attr" (attributes)
|
vartype is one of "prop" (database fields) or "attr" (attributes)
|
||||||
|
|
@ -296,7 +296,7 @@ def report(oobhandler, session, *args, **kwargs):
|
||||||
key, ReportAttributeTracker)
|
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.
|
This removes tracking for the given data given in *args.
|
||||||
vartype is one of of "prop" or "attr".
|
vartype is one of of "prop" or "attr".
|
||||||
|
|
@ -311,6 +311,7 @@ def unreport(oobhandler, session, vartype="prop", *args, **kwargs):
|
||||||
else: # assume attribute
|
else: # assume attribute
|
||||||
oobhandler.untrack_attribute(obj, session.sessid, key)
|
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"
|
"Test function, returning the args, kwargs"
|
||||||
session.msg(oob=("send", args, kwargs))
|
args = ["Return echo:"] + list(args)
|
||||||
|
session.msg(oob=("echo", args, kwargs))
|
||||||
|
|
|
||||||
|
|
@ -347,7 +347,7 @@ class OOBHandler(object):
|
||||||
using *args and **kwargs
|
using *args and **kwargs
|
||||||
"""
|
"""
|
||||||
try:
|
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 = _OOB_FUNCS[func_key] # raise traceback if not found
|
||||||
oobfunc(self, session, *args, **kwargs)
|
oobfunc(self, session, *args, **kwargs)
|
||||||
except KeyError,e:
|
except KeyError,e:
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ class WebSocketProtocol(Protocol, Session):
|
||||||
prefix.
|
prefix.
|
||||||
OOB - This is an Out-of-band instruction. If so,
|
OOB - This is an Out-of-band instruction. If so,
|
||||||
the remaining string should be a json-packed
|
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
|
any other prefix (or lack of prefix) is considered
|
||||||
plain text data, to be treated like a game
|
plain text data, to be treated like a game
|
||||||
input command.
|
input command.
|
||||||
|
|
@ -82,10 +82,8 @@ class WebSocketProtocol(Protocol, Session):
|
||||||
string = string[3:]
|
string = string[3:]
|
||||||
try:
|
try:
|
||||||
oobdata = json.loads(string)
|
oobdata = json.loads(string)
|
||||||
for (key, argstuple) in oobdata.items():
|
for (key, args) in oobdata.items():
|
||||||
args = argstuple[0] if argstuple else []
|
self.data_in(text=None, oob=(key, args))
|
||||||
kwargs = argstuple[1] if len(argstuple) > 1 else {}
|
|
||||||
self.data_in(text=None, oob=(key, args, kwargs))
|
|
||||||
except Exception:
|
except Exception:
|
||||||
log_trace("Websocket malformed OOB request: %s" % string)
|
log_trace("Websocket malformed OOB request: %s" % string)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,14 @@ class SessionHandler(object):
|
||||||
"""
|
"""
|
||||||
Helper method for each session to use to parse oob structures
|
Helper method for each session to use to parse oob structures
|
||||||
(The 'oob' kwarg of the msg() method)
|
(The 'oob' kwarg of the msg() method)
|
||||||
|
((cmdname, (args), {}), ...)
|
||||||
|
|
||||||
|
|
||||||
|
Allowed oob structures are:
|
||||||
allowed oob structures are
|
allowed oob structures are
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cmdname
|
cmdname
|
||||||
((cmdname,), (cmdname,))
|
((cmdname,), (cmdname,))
|
||||||
(cmdname,(arg, ))
|
(cmdname,(arg, ))
|
||||||
|
|
@ -134,10 +141,10 @@ class SessionHandler(object):
|
||||||
return (oobstruct[0].lower(), (), dict(oobstruct[1]))
|
return (oobstruct[0].lower(), (), dict(oobstruct[1]))
|
||||||
elif isinstance(oobstruct[1], (tuple, list)):
|
elif isinstance(oobstruct[1], (tuple, list)):
|
||||||
# cmdname, (args,)
|
# cmdname, (args,)
|
||||||
return (oobstruct[0].lower(), tuple(oobstruct[1]), {})
|
return (oobstruct[0].lower(), list(oobstruct[1]), {})
|
||||||
else:
|
else:
|
||||||
# cmdname, (args,), {kwargs}
|
# 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__"):
|
if hasattr(oobstruct, "__iter__"):
|
||||||
# differentiate between (cmdname, cmdname),
|
# differentiate between (cmdname, cmdname),
|
||||||
|
|
@ -145,12 +152,12 @@ class SessionHandler(object):
|
||||||
# (cmdname,args,kwargs), ...)
|
# (cmdname,args,kwargs), ...)
|
||||||
|
|
||||||
if oobstruct and isinstance(oobstruct[0], basestring):
|
if oobstruct and isinstance(oobstruct[0], basestring):
|
||||||
return (tuple(_parse(oobstruct)),)
|
return (list(_parse(oobstruct)),)
|
||||||
else:
|
else:
|
||||||
out = []
|
out = []
|
||||||
for oobpart in oobstruct:
|
for oobpart in oobstruct:
|
||||||
out.append(_parse(oobpart))
|
out.append(_parse(oobpart))
|
||||||
return (tuple(out),)
|
return (list(out),)
|
||||||
return (_parse(oobstruct),)
|
return (_parse(oobstruct),)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ messages sent to the client is one of three modes:
|
||||||
var wsurl = "ws://localhost:8001";
|
var wsurl = "ws://localhost:8001";
|
||||||
|
|
||||||
function webclient_init(){
|
function webclient_init(){
|
||||||
|
// initializing the client once the html page has loaded
|
||||||
websocket = new WebSocket(wsurl);
|
websocket = new WebSocket(wsurl);
|
||||||
websocket.onopen = function(evt) { onOpen(evt) };
|
websocket.onopen = function(evt) { onOpen(evt) };
|
||||||
websocket.onclose = function(evt) { onClose(evt) };
|
websocket.onclose = function(evt) { onClose(evt) };
|
||||||
|
|
@ -33,6 +34,7 @@ function webclient_init(){
|
||||||
}
|
}
|
||||||
|
|
||||||
function onOpen(evt) {
|
function onOpen(evt) {
|
||||||
|
// client is just connecting
|
||||||
$("#connecting").remove(); // remove the "connecting ..." message
|
$("#connecting").remove(); // remove the "connecting ..." message
|
||||||
msg_display("sys", "Using websockets - connected to " + wsurl + ".")
|
msg_display("sys", "Using websockets - connected to " + wsurl + ".")
|
||||||
|
|
||||||
|
|
@ -42,24 +44,25 @@ function onOpen(evt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClose(evt) {
|
function onClose(evt) {
|
||||||
|
// client is closing
|
||||||
CLIENT_HASH = 0;
|
CLIENT_HASH = 0;
|
||||||
alert("Mud client connection was closed cleanly.");
|
alert("Mud client connection was closed cleanly.");
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(evt) {
|
function onMessage(evt) {
|
||||||
|
// outgoing message from server
|
||||||
var inmsg = evt.data
|
var inmsg = evt.data
|
||||||
if (inmsg.length > 3 && inmsg.substr(0, 3) == "OOB") {
|
if (inmsg.length > 3 && inmsg.substr(0, 3) == "OOB") {
|
||||||
// dynamically call oob methods, if available
|
// 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) {
|
catch(err) {
|
||||||
// not JSON packed - a normal text
|
// not JSON packed - a normal text
|
||||||
msg_display('out', inmsg);
|
msg_display('out', err + " " + inmsg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
for (var ind in oobarray) {
|
||||||
for (var oobtuple in oobtuples) {
|
try { window[oobarray[ind][0]](oobarray[ind][1]) }
|
||||||
try { window[oobtuple[0]](oobtuple[1]) }
|
catch(err) { msg_display("err", "Could not execute OOB function " + oobtuple[0] + "(" + oobtuple[1] + ")!") }
|
||||||
catch(err) { msg_display("err", "Could not execute OOB function " + oobtuple[0] + "!") }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -68,10 +71,12 @@ function onMessage(evt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(evt) {
|
function onError(evt) {
|
||||||
|
// client error message
|
||||||
msg_display('err', "Error: Server returned an error. Try reloading the page.");
|
msg_display('err', "Error: Server returned an error. Try reloading the page.");
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSend(){
|
function doSend(){
|
||||||
|
// sending data from client to server
|
||||||
outmsg = $("#inputfield").val();
|
outmsg = $("#inputfield").val();
|
||||||
history_add(outmsg);
|
history_add(outmsg);
|
||||||
HISTORY_POS = 0;
|
HISTORY_POS = 0;
|
||||||
|
|
@ -84,12 +89,23 @@ function doSend(){
|
||||||
websocket.send(outmsg); }
|
websocket.send(outmsg); }
|
||||||
}
|
}
|
||||||
|
|
||||||
function doOOB(ooblist){
|
function doOOB(oobdict){
|
||||||
// Takes an array on form [funcname, [args], funcname, [args], ... ]
|
// Handle OOB communication from client side
|
||||||
var oobmsg = JSON.stringify(ooblist);
|
// 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);
|
websocket.send("OOB" + oobmsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// OOB functions
|
||||||
|
//
|
||||||
|
|
||||||
|
function echo(message) {
|
||||||
|
msg_display("out", "ECHO return: " + message) }
|
||||||
|
|
||||||
//
|
//
|
||||||
// Display messages
|
// Display messages
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue