mirror of
https://github.com/evennia/evennia.git
synced 2026-03-27 02:06:32 +01:00
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)
|
||||
session.update_session_counters()
|
||||
|
||||
def echo(session, *args, **kwargs):
|
||||
session.data_out(text=(args, kwargs))
|
||||
|
||||
def default(session, cmdname, *args, **kwargs):
|
||||
"""
|
||||
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)
|
||||
return
|
||||
# scrub data
|
||||
print ("portalsessionhandler before clean:", session, kwargs)
|
||||
kwargs = self.clean_senddata(session, kwargs)
|
||||
print ("portalsessionhandler after clean:", session, kwargs)
|
||||
|
||||
# relay data to Server
|
||||
self.command_counter += 1
|
||||
|
|
|
|||
|
|
@ -55,10 +55,6 @@ class WebSocketClient(Protocol, Session):
|
|||
self.transport.setTcpKeepAlive(1)
|
||||
self.sessionhandler.connect(self)
|
||||
|
||||
self.datamap = {"text": self.send_text,
|
||||
"prompt": self.send_prompt,
|
||||
"_default": self.data_oob}
|
||||
|
||||
def disconnect(self, reason=None):
|
||||
"""
|
||||
Generic hook for the engine to call in order to
|
||||
|
|
@ -95,8 +91,9 @@ class WebSocketClient(Protocol, Session):
|
|||
|
||||
"""
|
||||
cmdarray = json.loads(string)
|
||||
print "dataReceived:", cmdarray
|
||||
if cmdarray:
|
||||
self.data_in(**{cmdarray[0]:cmdarray[1:]})
|
||||
self.data_in(**{cmdarray[0]:[cmdarray[1], cmdarray[2]]})
|
||||
|
||||
def sendLine(self, line):
|
||||
"""
|
||||
|
|
@ -129,8 +126,7 @@ class WebSocketClient(Protocol, Session):
|
|||
"""
|
||||
self.sessionhandler.data_out(self, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def send_text(session, *args, **kwargs):
|
||||
def send_text(self, *args, **kwargs):
|
||||
"""
|
||||
Send text data. This will pre-process the text for
|
||||
color-replacement, conversion to html etc.
|
||||
|
|
@ -165,25 +161,23 @@ class WebSocketClient(Protocol, Session):
|
|||
|
||||
if raw:
|
||||
# no processing
|
||||
data = json.dumps((text,) + args)
|
||||
data = json.dumps([cmd, (text,) + args, kwargs])
|
||||
else:
|
||||
# send normally, with html processing
|
||||
data = json.dumps((cmd, parse_html(text, strip_ansi=nomarkup)) + args)
|
||||
session.sendLine(data)
|
||||
data = json.dumps([cmd, (parse_html(text, strip_ansi=nomarkup),) + args, kwargs])
|
||||
self.sendLine(data)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def send_prompt(session, *args, **kwargs):
|
||||
def send_prompt(self, *args, **kwargs):
|
||||
kwargs["options"].update({"send_prompt": True})
|
||||
session.send_text(*args, **kwargs)
|
||||
self.send_text(*args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def send_oob(session, *args, **kwargs):
|
||||
def send_default(session, cmdname, *args, **kwargs):
|
||||
"""
|
||||
Data Evennia -> User.
|
||||
|
||||
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`.
|
||||
|
||||
Kwargs:
|
||||
|
|
@ -192,5 +186,4 @@ class WebSocketClient(Protocol, Session):
|
|||
client instead.
|
||||
|
||||
"""
|
||||
if args:
|
||||
session.sendLine(json.dumps(args))
|
||||
session.sendLine(json.dumps([cmdname, args, kwargs]))
|
||||
|
|
|
|||
|
|
@ -175,6 +175,7 @@ class SessionHandler(dict):
|
|||
|
||||
rkwargs = {}
|
||||
for key, data in kwargs.iteritems():
|
||||
print "sessionhandler.clean_senddata:", key, data
|
||||
if not data:
|
||||
rkwargs[key] = [ [], {} ]
|
||||
elif isinstance(data, dict):
|
||||
|
|
|
|||
|
|
@ -25,24 +25,24 @@ following official functions:
|
|||
compatibility. See below.
|
||||
'emitter': An optional custom command handler for distributing
|
||||
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)
|
||||
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).
|
||||
|
||||
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
|
||||
the conversion to JSON before sending across the wire.
|
||||
- When receiving data from the Server (always [cmdname, kwargs]), this must be
|
||||
the conversion to JSON before sending across the wire.
|
||||
- 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]).
|
||||
An "emitter" object must have a function
|
||||
An "emitter" object must have a function
|
||||
- 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
|
||||
should be called as listener(kwargs) when the backend calls emit.
|
||||
- off(cmdname) - remove the listener for this cmdname.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
(function() {
|
||||
|
|
@ -57,17 +57,17 @@ An "emitter" object must have a function
|
|||
// startup Evennia emitter and connection.
|
||||
//
|
||||
// Args:
|
||||
// opts (obj):
|
||||
// opts (obj):
|
||||
// emitter - custom emitter. If not given,
|
||||
// will use a default emitter. Must have
|
||||
// will use a default emitter. Must have
|
||||
// an "emit" function.
|
||||
// connection - This defaults to using either
|
||||
// connection - This defaults to using either
|
||||
// a WebsocketConnection or a CometConnection
|
||||
// depending on what the browser supports. If given
|
||||
// it must have a 'msg' method and make use of
|
||||
// Evennia.emit to return data to Client.
|
||||
//
|
||||
init: function(opts) {
|
||||
init: function(opts) {
|
||||
opts = opts || {};
|
||||
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();
|
||||
},
|
||||
|
||||
// Client -> Evennia.
|
||||
|
||||
// Client -> Evennia.
|
||||
// Called by the frontend to send a command to Evennia.
|
||||
//
|
||||
// Args:
|
||||
|
|
@ -93,17 +93,19 @@ An "emitter" object must have a function
|
|||
// kwargs (obj): Data argument for calling as cmdname(kwargs)
|
||||
// callback (func): If given, will be given an eventual return
|
||||
// value from the backend.
|
||||
//
|
||||
msg: function (cmdname, kwargs, callback) {
|
||||
//
|
||||
msg: function (cmdname, args, kwargs, callback) {
|
||||
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') {
|
||||
cmdmap[cmdid] = callback;
|
||||
}
|
||||
this.connection.msg(data);
|
||||
|
||||
log('cmd called with following args: ' + cmdname, ', ' + kwargs + ', ' + callback);
|
||||
log('client msg sending: ' + cmdname + " " + args + " " + outargs + " " + outkwargs);
|
||||
},
|
||||
|
||||
// Evennia -> Client.
|
||||
|
|
@ -113,7 +115,7 @@ An "emitter" object must have a function
|
|||
//
|
||||
// Args:
|
||||
// event (event): Event received from Evennia
|
||||
// data (obj):
|
||||
// data (obj):
|
||||
//
|
||||
emit: function (cmdname, data) {
|
||||
log('emit called with args: + ' + cmdname + ',' + data);
|
||||
|
|
@ -128,7 +130,7 @@ An "emitter" object must have a function
|
|||
|
||||
}; // end of evennia object
|
||||
|
||||
|
||||
|
||||
// Basic emitter to distribute data being sent to the client from
|
||||
// the Server. An alternative can be overridden in Evennia.init.
|
||||
//
|
||||
|
|
@ -155,7 +157,7 @@ An "emitter" object must have a function
|
|||
// Args:
|
||||
// cmdname (str): Name of event to handle.
|
||||
// listener (function): Function taking one argument,
|
||||
// to listen to cmdname events.
|
||||
// to listen to cmdname events.
|
||||
//
|
||||
var on = function (cmdname, listener) {
|
||||
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
|
||||
// the clients.
|
||||
// the clients.
|
||||
$.ajax({type: "POST", url: "/webclientdata",
|
||||
async: true, cache: false, timeout: 50000,
|
||||
datatype: "json",
|
||||
|
|
@ -283,7 +285,7 @@ function log(msg) {
|
|||
|
||||
// Called when page has finished loading (kicks the client into gear)
|
||||
$(document).ready(function(){
|
||||
|
||||
|
||||
// a small timeout to stop 'loading' indicator in Chrome
|
||||
setTimeout(function () {
|
||||
log('Evennia initialized...')
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{% extends "webclient/base.html" %}
|
||||
|
||||
{% block connecting %}
|
||||
{% block connecting %}
|
||||
{% endblock %}
|
||||
|
||||
{% block client %}
|
||||
|
|
@ -8,8 +8,18 @@
|
|||
<h1>Webclient!</h1>
|
||||
|
||||
<form>
|
||||
<input type="text" name="input">
|
||||
<input type="button" value="Send" onClick="Evennia.msg">
|
||||
<input id="maininput" type="text" name="input">
|
||||
<input type="button" value="Send" onClick="sendInput()">
|
||||
</form>
|
||||
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
var sendInput = function() {
|
||||
var inmsg = $("#maininput").val();
|
||||
log("sendInput: " + inmsg);
|
||||
Evennia.msg("text", [inmsg], {});
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue