Added close-handlers to websocket and did some other cleanup.

This commit is contained in:
Griatch 2016-02-14 13:52:08 +01:00
parent 166189a7a5
commit b6642cdfe9
4 changed files with 20 additions and 5 deletions

View file

@ -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):

View file

@ -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)

View file

@ -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;
};

View file

@ -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,