From b6642cdfe96573458b76b62b23d7b6d8f985a9ea Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 14 Feb 2016 13:52:08 +0100 Subject: [PATCH] Added close-handlers to websocket and did some other cleanup. --- evennia/server/portal/webclient.py | 14 +++++++++++--- evennia/server/portal/webclient_ajax.py | 3 ++- .../web/webclient/static/webclient/js/evennia.js | 5 ++++- .../webclient/static/webclient/js/webclient_gui.js | 3 +++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/evennia/server/portal/webclient.py b/evennia/server/portal/webclient.py index 5b8f716bdb..d0d9f262d0 100644 --- a/evennia/server/portal/webclient.py +++ b/evennia/server/portal/webclient.py @@ -72,7 +72,7 @@ class WebSocketClient(Protocol, Session): """ This is executed when the connection is lost for whatever reason. it can also be called directly, from the disconnect - method + method. Args: reason (str): Motivation for the lost connection. @@ -91,9 +91,8 @@ 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): """ @@ -114,7 +113,16 @@ class WebSocketClient(Protocol, Session): text (str): Incoming text. kwargs (any): Options from protocol. + Notes: + The websocket client can send the + "websocket_close" command to report + that the client has been closed and + that the session should be disconnected. + """ + if "websocket_close" in kwargs: + self.disconnect() + return self.sessionhandler.data_in(self, **kwargs) def data_out(self, **kwargs): diff --git a/evennia/server/portal/webclient_ajax.py b/evennia/server/portal/webclient_ajax.py index 2a2759fc5c..9280ec30ee 100644 --- a/evennia/server/portal/webclient_ajax.py +++ b/evennia/server/portal/webclient_ajax.py @@ -278,12 +278,13 @@ class WebClientSession(session.Session): """ # string handling is similar to telnet + if args: args = list(args) text = args[0] if text is None: return - text = utils.to_str(text, force_string=True) + text = utils.to_str(text, force_string=True) options = kwargs.get("options", {}) raw = options.get("raw", False) diff --git a/evennia/web/webclient/static/webclient/js/evennia.js b/evennia/web/webclient/static/webclient/js/evennia.js index d41b3fc81a..24c237bf21 100644 --- a/evennia/web/webclient/static/webclient/js/evennia.js +++ b/evennia/web/webclient/static/webclient/js/evennia.js @@ -231,7 +231,10 @@ An "emitter" object must have a function websocket.send(JSON.stringify(data)); }; websocket.close = function() { - // close connection. + // tell the server this connection is closing (usually + // tied to when the client window is closed). This + // Makes use of a websocket-protocol specific instruction. + websocket.send(JSON.stringify(["websocket_close", [], {}])); } return websocket; }; diff --git a/evennia/web/webclient/static/webclient/js/webclient_gui.js b/evennia/web/webclient/static/webclient/js/webclient_gui.js index d43562a0ee..b78fa11aa8 100644 --- a/evennia/web/webclient/static/webclient/js/webclient_gui.js +++ b/evennia/web/webclient/static/webclient/js/webclient_gui.js @@ -138,6 +138,9 @@ $(document).ready(function() { Evennia.emitter.on("text", onText); Evennia.emitter.on("prompt", onPrompt); Evennia.emitter.on("default", onDefault); + // Event when closing window (have to have Evennia initialized) + $(window).bind("beforeunload", Evennia.connection.close); + doWindowResize(); // set an idle timer to send idle every 3 minutes,