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 This is executed when the connection is lost for whatever
reason. it can also be called directly, from the disconnect reason. it can also be called directly, from the disconnect
method method.
Args: Args:
reason (str): Motivation for the lost connection. reason (str): Motivation for the lost connection.
@ -91,9 +91,8 @@ 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], cmdarray[2]]}) self.data_in(**{cmdarray[0]:[cmdarray[1], cmdarray[2]]})
def sendLine(self, line): def sendLine(self, line):
""" """
@ -114,7 +113,16 @@ class WebSocketClient(Protocol, Session):
text (str): Incoming text. text (str): Incoming text.
kwargs (any): Options from protocol. 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) self.sessionhandler.data_in(self, **kwargs)
def data_out(self, **kwargs): def data_out(self, **kwargs):

View file

@ -278,6 +278,7 @@ class WebClientSession(session.Session):
""" """
# string handling is similar to telnet # string handling is similar to telnet
if args: if args:
args = list(args) args = list(args)
text = args[0] text = args[0]

View file

@ -231,7 +231,10 @@ An "emitter" object must have a function
websocket.send(JSON.stringify(data)); websocket.send(JSON.stringify(data));
}; };
websocket.close = function() { 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; return websocket;
}; };

View file

@ -138,6 +138,9 @@ $(document).ready(function() {
Evennia.emitter.on("text", onText); Evennia.emitter.on("text", onText);
Evennia.emitter.on("prompt", onPrompt); Evennia.emitter.on("prompt", onPrompt);
Evennia.emitter.on("default", onDefault); Evennia.emitter.on("default", onDefault);
// Event when closing window (have to have Evennia initialized)
$(window).bind("beforeunload", Evennia.connection.close);
doWindowResize(); doWindowResize();
// set an idle timer to send idle every 3 minutes, // set an idle timer to send idle every 3 minutes,