Still messing with getting the webclient to talk properly to the server.
This commit is contained in:
parent
c511263f63
commit
2890371900
6 changed files with 57 additions and 46 deletions
|
|
@ -105,6 +105,9 @@ def text(session, *args, **kwargs):
|
||||||
cmdhandler(session, text, callertype="session", session=session)
|
cmdhandler(session, text, callertype="session", session=session)
|
||||||
session.update_session_counters()
|
session.update_session_counters()
|
||||||
|
|
||||||
|
def echo(session, *args, **kwargs):
|
||||||
|
session.data_out(text=(args, kwargs))
|
||||||
|
|
||||||
def default(session, cmdname, *args, **kwargs):
|
def default(session, cmdname, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Default catch-function. This is like all other input functions except
|
Default catch-function. This is like all other input functions except
|
||||||
|
|
|
||||||
|
|
@ -334,7 +334,9 @@ class PortalSessionHandler(SessionHandler):
|
||||||
self.data_out(session, text=_ERROR_COMMAND_OVERFLOW)
|
self.data_out(session, text=_ERROR_COMMAND_OVERFLOW)
|
||||||
return
|
return
|
||||||
# scrub data
|
# scrub data
|
||||||
|
print ("portalsessionhandler before clean:", session, kwargs)
|
||||||
kwargs = self.clean_senddata(session, kwargs)
|
kwargs = self.clean_senddata(session, kwargs)
|
||||||
|
print ("portalsessionhandler after clean:", session, kwargs)
|
||||||
|
|
||||||
# relay data to Server
|
# relay data to Server
|
||||||
self.command_counter += 1
|
self.command_counter += 1
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,6 @@ class WebSocketClient(Protocol, Session):
|
||||||
self.transport.setTcpKeepAlive(1)
|
self.transport.setTcpKeepAlive(1)
|
||||||
self.sessionhandler.connect(self)
|
self.sessionhandler.connect(self)
|
||||||
|
|
||||||
self.datamap = {"text": self.send_text,
|
|
||||||
"prompt": self.send_prompt,
|
|
||||||
"_default": self.data_oob}
|
|
||||||
|
|
||||||
def disconnect(self, reason=None):
|
def disconnect(self, reason=None):
|
||||||
"""
|
"""
|
||||||
Generic hook for the engine to call in order to
|
Generic hook for the engine to call in order to
|
||||||
|
|
@ -95,8 +91,9 @@ class WebSocketClient(Protocol, Session):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
cmdarray = json.loads(string)
|
cmdarray = json.loads(string)
|
||||||
|
print "dataReceived:", cmdarray
|
||||||
if cmdarray:
|
if cmdarray:
|
||||||
self.data_in(**{cmdarray[0]:cmdarray[1:]})
|
self.data_in(**{cmdarray[0]:[cmdarray[1], cmdarray[2]]})
|
||||||
|
|
||||||
def sendLine(self, line):
|
def sendLine(self, line):
|
||||||
"""
|
"""
|
||||||
|
|
@ -129,8 +126,7 @@ class WebSocketClient(Protocol, Session):
|
||||||
"""
|
"""
|
||||||
self.sessionhandler.data_out(self, **kwargs)
|
self.sessionhandler.data_out(self, **kwargs)
|
||||||
|
|
||||||
@staticmethod
|
def send_text(self, *args, **kwargs):
|
||||||
def send_text(session, *args, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
Send text data. This will pre-process the text for
|
Send text data. This will pre-process the text for
|
||||||
color-replacement, conversion to html etc.
|
color-replacement, conversion to html etc.
|
||||||
|
|
@ -165,25 +161,23 @@ class WebSocketClient(Protocol, Session):
|
||||||
|
|
||||||
if raw:
|
if raw:
|
||||||
# no processing
|
# no processing
|
||||||
data = json.dumps((text,) + args)
|
data = json.dumps([cmd, (text,) + args, kwargs])
|
||||||
else:
|
else:
|
||||||
# send normally, with html processing
|
# send normally, with html processing
|
||||||
data = json.dumps((cmd, parse_html(text, strip_ansi=nomarkup)) + args)
|
data = json.dumps([cmd, (parse_html(text, strip_ansi=nomarkup),) + args, kwargs])
|
||||||
session.sendLine(data)
|
self.sendLine(data)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
def send_prompt(self, *args, **kwargs):
|
||||||
def send_prompt(session, *args, **kwargs):
|
|
||||||
kwargs["options"].update({"send_prompt": True})
|
kwargs["options"].update({"send_prompt": True})
|
||||||
session.send_text(*args, **kwargs)
|
self.send_text(*args, **kwargs)
|
||||||
|
|
||||||
@staticmethod
|
def send_default(session, cmdname, *args, **kwargs):
|
||||||
def send_oob(session, *args, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
Data Evennia -> User.
|
Data Evennia -> User.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
cmd (str): The first argument will always be the oob cmd name.
|
cmdname (str): The first argument will always be the oob cmd name.
|
||||||
*args (any): Remaining args will be arguments for `cmd`.
|
*args (any): Remaining args will be arguments for `cmd`.
|
||||||
|
|
||||||
Kwargs:
|
Kwargs:
|
||||||
|
|
@ -192,5 +186,4 @@ class WebSocketClient(Protocol, Session):
|
||||||
client instead.
|
client instead.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if args:
|
session.sendLine(json.dumps([cmdname, args, kwargs]))
|
||||||
session.sendLine(json.dumps(args))
|
|
||||||
|
|
|
||||||
|
|
@ -175,6 +175,7 @@ class SessionHandler(dict):
|
||||||
|
|
||||||
rkwargs = {}
|
rkwargs = {}
|
||||||
for key, data in kwargs.iteritems():
|
for key, data in kwargs.iteritems():
|
||||||
|
print "sessionhandler.clean_senddata:", key, data
|
||||||
if not data:
|
if not data:
|
||||||
rkwargs[key] = [ [], {} ]
|
rkwargs[key] = [ [], {} ]
|
||||||
elif isinstance(data, dict):
|
elif isinstance(data, dict):
|
||||||
|
|
|
||||||
|
|
@ -25,24 +25,24 @@ following official functions:
|
||||||
compatibility. See below.
|
compatibility. See below.
|
||||||
'emitter': An optional custom command handler for distributing
|
'emitter': An optional custom command handler for distributing
|
||||||
data from the server to suitable listeners. If not given,
|
data from the server to suitable listeners. If not given,
|
||||||
a default will be used.
|
a default will be used.
|
||||||
- Evennia.msg(funcname, [args,...], callback)
|
- Evennia.msg(funcname, [args,...], callback)
|
||||||
Send a command to the server. You can also provide a function
|
Send a command to the server. You can also provide a function
|
||||||
to call with the return of the call (note that commands will
|
to call with the return of the call (note that commands will
|
||||||
not return anything unless specified to do so server-side).
|
not return anything unless specified to do so server-side).
|
||||||
|
|
||||||
A "connection" object must have the method
|
A "connection" object must have the method
|
||||||
- msg(data) - this should relay data to the Server. This function should itself handle
|
- msg(data) - this should relay data to the Server. This function should itself handle
|
||||||
the conversion to JSON before sending across the wire.
|
the conversion to JSON before sending across the wire.
|
||||||
- When receiving data from the Server (always [cmdname, kwargs]), this must be
|
- When receiving data from the Server (always [cmdname, kwargs]), this must be
|
||||||
JSON-unpacked and the result redirected to Evennia.emit(data[0], data[1]).
|
JSON-unpacked and the result redirected to Evennia.emit(data[0], data[1]).
|
||||||
An "emitter" object must have a function
|
An "emitter" object must have a function
|
||||||
- emit(cmdname, kwargs) - this will be called by the backend.
|
- emit(cmdname, kwargs) - this will be called by the backend.
|
||||||
- The default emitter also has the following methods:
|
- The default emitter also has the following methods:
|
||||||
- on(cmdname, listener) - this ties a listener to the backend. This function
|
- on(cmdname, listener) - this ties a listener to the backend. This function
|
||||||
should be called as listener(kwargs) when the backend calls emit.
|
should be called as listener(kwargs) when the backend calls emit.
|
||||||
- off(cmdname) - remove the listener for this cmdname.
|
- off(cmdname) - remove the listener for this cmdname.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
@ -57,17 +57,17 @@ An "emitter" object must have a function
|
||||||
// startup Evennia emitter and connection.
|
// startup Evennia emitter and connection.
|
||||||
//
|
//
|
||||||
// Args:
|
// Args:
|
||||||
// opts (obj):
|
// opts (obj):
|
||||||
// emitter - custom emitter. If not given,
|
// emitter - custom emitter. If not given,
|
||||||
// will use a default emitter. Must have
|
// will use a default emitter. Must have
|
||||||
// an "emit" function.
|
// an "emit" function.
|
||||||
// connection - This defaults to using either
|
// connection - This defaults to using either
|
||||||
// a WebsocketConnection or a CometConnection
|
// a WebsocketConnection or a CometConnection
|
||||||
// depending on what the browser supports. If given
|
// depending on what the browser supports. If given
|
||||||
// it must have a 'msg' method and make use of
|
// it must have a 'msg' method and make use of
|
||||||
// Evennia.emit to return data to Client.
|
// Evennia.emit to return data to Client.
|
||||||
//
|
//
|
||||||
init: function(opts) {
|
init: function(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
this.emitter = opts.emitter || new DefaultEmitter();
|
this.emitter = opts.emitter || new DefaultEmitter();
|
||||||
|
|
||||||
|
|
@ -84,8 +84,8 @@ An "emitter" object must have a function
|
||||||
}
|
}
|
||||||
// this.connection = opts.connection || window.WebSocket ? new WebsocketConnection() : new AjaxCometConnection();
|
// this.connection = opts.connection || window.WebSocket ? new WebsocketConnection() : new AjaxCometConnection();
|
||||||
},
|
},
|
||||||
|
|
||||||
// Client -> Evennia.
|
// Client -> Evennia.
|
||||||
// Called by the frontend to send a command to Evennia.
|
// Called by the frontend to send a command to Evennia.
|
||||||
//
|
//
|
||||||
// Args:
|
// Args:
|
||||||
|
|
@ -93,17 +93,19 @@ An "emitter" object must have a function
|
||||||
// kwargs (obj): Data argument for calling as cmdname(kwargs)
|
// kwargs (obj): Data argument for calling as cmdname(kwargs)
|
||||||
// callback (func): If given, will be given an eventual return
|
// callback (func): If given, will be given an eventual return
|
||||||
// value from the backend.
|
// value from the backend.
|
||||||
//
|
//
|
||||||
msg: function (cmdname, kwargs, callback) {
|
msg: function (cmdname, args, kwargs, callback) {
|
||||||
kwargs.cmdid = cmdid++;
|
kwargs.cmdid = cmdid++;
|
||||||
var data = kwargs ? [cmdname, kwargs] : [cmdname, {}];
|
var outargs = args ? args : [];
|
||||||
|
var outkwargs = kwargs ? kwargs : {};
|
||||||
|
var data = [cmdname, outargs, outkwargs];
|
||||||
|
|
||||||
if (typeof callback === 'function') {
|
if (typeof callback === 'function') {
|
||||||
cmdmap[cmdid] = callback;
|
cmdmap[cmdid] = callback;
|
||||||
}
|
}
|
||||||
this.connection.msg(data);
|
this.connection.msg(data);
|
||||||
|
|
||||||
log('cmd called with following args: ' + cmdname, ', ' + kwargs + ', ' + callback);
|
log('client msg sending: ' + cmdname + " " + args + " " + outargs + " " + outkwargs);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Evennia -> Client.
|
// Evennia -> Client.
|
||||||
|
|
@ -113,7 +115,7 @@ An "emitter" object must have a function
|
||||||
//
|
//
|
||||||
// Args:
|
// Args:
|
||||||
// event (event): Event received from Evennia
|
// event (event): Event received from Evennia
|
||||||
// data (obj):
|
// data (obj):
|
||||||
//
|
//
|
||||||
emit: function (cmdname, data) {
|
emit: function (cmdname, data) {
|
||||||
log('emit called with args: + ' + cmdname + ',' + data);
|
log('emit called with args: + ' + cmdname + ',' + data);
|
||||||
|
|
@ -128,7 +130,7 @@ An "emitter" object must have a function
|
||||||
|
|
||||||
}; // end of evennia object
|
}; // end of evennia object
|
||||||
|
|
||||||
|
|
||||||
// Basic emitter to distribute data being sent to the client from
|
// Basic emitter to distribute data being sent to the client from
|
||||||
// the Server. An alternative can be overridden in Evennia.init.
|
// the Server. An alternative can be overridden in Evennia.init.
|
||||||
//
|
//
|
||||||
|
|
@ -155,7 +157,7 @@ An "emitter" object must have a function
|
||||||
// Args:
|
// Args:
|
||||||
// cmdname (str): Name of event to handle.
|
// cmdname (str): Name of event to handle.
|
||||||
// listener (function): Function taking one argument,
|
// listener (function): Function taking one argument,
|
||||||
// to listen to cmdname events.
|
// to listen to cmdname events.
|
||||||
//
|
//
|
||||||
var on = function (cmdname, listener) {
|
var on = function (cmdname, listener) {
|
||||||
if (typeof(listener === 'function')) {
|
if (typeof(listener === 'function')) {
|
||||||
|
|
@ -248,10 +250,10 @@ An "emitter" object must have a function
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialization will happen when this Connection is created.
|
// Initialization will happen when this Connection is created.
|
||||||
// We need to store the client id so Evennia knows to separate
|
// We need to store the client id so Evennia knows to separate
|
||||||
// the clients.
|
// the clients.
|
||||||
$.ajax({type: "POST", url: "/webclientdata",
|
$.ajax({type: "POST", url: "/webclientdata",
|
||||||
async: true, cache: false, timeout: 50000,
|
async: true, cache: false, timeout: 50000,
|
||||||
datatype: "json",
|
datatype: "json",
|
||||||
|
|
@ -283,7 +285,7 @@ function log(msg) {
|
||||||
|
|
||||||
// Called when page has finished loading (kicks the client into gear)
|
// Called when page has finished loading (kicks the client into gear)
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
|
|
||||||
// a small timeout to stop 'loading' indicator in Chrome
|
// a small timeout to stop 'loading' indicator in Chrome
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
log('Evennia initialized...')
|
log('Evennia initialized...')
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "webclient/base.html" %}
|
{% extends "webclient/base.html" %}
|
||||||
|
|
||||||
{% block connecting %}
|
{% block connecting %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block client %}
|
{% block client %}
|
||||||
|
|
@ -8,8 +8,18 @@
|
||||||
<h1>Webclient!</h1>
|
<h1>Webclient!</h1>
|
||||||
|
|
||||||
<form>
|
<form>
|
||||||
<input type="text" name="input">
|
<input id="maininput" type="text" name="input">
|
||||||
<input type="button" value="Send" onClick="Evennia.msg">
|
<input type="button" value="Send" onClick="sendInput()">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<script language="javascript" type="text/javascript">
|
||||||
|
|
||||||
|
var sendInput = function() {
|
||||||
|
var inmsg = $("#maininput").val();
|
||||||
|
log("sendInput: " + inmsg);
|
||||||
|
Evennia.msg("text", [inmsg], {});
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue