Silenced the unhandled events from websocket connections.

This commit is contained in:
Griatch 2016-02-14 15:29:31 +01:00
parent b6642cdfe9
commit b11c746f8f
2 changed files with 23 additions and 21 deletions

View file

@ -58,8 +58,7 @@ An "emitter" object must have a function
debug: true, debug: true,
initialized: false, initialized: false,
// Initialize. // Initialize the Evennia object with emitter and connection.
// startup Evennia emitter and connection.
// //
// Args: // Args:
// opts (obj): // opts (obj):
@ -82,24 +81,21 @@ An "emitter" object must have a function
opts = opts || {}; opts = opts || {};
this.emitter = opts.emitter || new DefaultEmitter(); this.emitter = opts.emitter || new DefaultEmitter();
if (opts.connection) { if (opts.ckonnection) {
this.connection = opts.connection; this.connection = opts.connection;
} }
else if (window.WebSocket && wsactive) { else if (window.WebSocket && wsactive) {
this.connection = new WebsocketConnection(); this.connection = new WebsocketConnection();
if (!this.connection) {
this.connection = new AjaxCometConnection();
}
} else { } else {
this.connection = new AjaxCometConnection(); this.connection = new AjaxCometConnection();
} }
log('Evennia initialized.') log('Evennia initialized.')
}, },
// 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:
// cmdname (str): String identifier to call // cmdname (str): String identifier to call
// 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
@ -191,7 +187,7 @@ An "emitter" object must have a function
var off = function (cmdname) { var off = function (cmdname) {
delete listeners[cmdname] delete listeners[cmdname]
}; };
return {emit:emit, on:on, off:off} return {emit:emit, on:on, off:off};
}; };
// Websocket Connector // Websocket Connector
@ -201,15 +197,15 @@ An "emitter" object must have a function
var websocket = new WebSocket(wsurl); var websocket = new WebSocket(wsurl);
// Handle Websocket open event // Handle Websocket open event
websocket.onopen = function (event) { websocket.onopen = function (event) {
Evennia.emit('connection.open', ["websocket"], event); Evennia.emit('connection_open', ["websocket"], event);
}; };
// Handle Websocket close event // Handle Websocket close event
websocket.onclose = function (event) { websocket.onclose = function (event) {
Evennia.emit('connection.close', ["websocket"], event); Evennia.emit('connection_close', ["websocket"], event);
}; };
// Handle websocket errors // Handle websocket errors
websocket.onerror = function (event) { websocket.onerror = function (event) {
Evennia.emit('connection.error', ["websocket"], event); Evennia.emit('connection_error', ["websocket"], event);
if (websocket.readyState === websocket.CLOSED) { if (websocket.readyState === websocket.CLOSED) {
log("Websocket failed. Falling back to Ajax..."); log("Websocket failed. Falling back to Ajax...");
Evennia.connection = AjaxCometConnection(); Evennia.connection = AjaxCometConnection();
@ -254,12 +250,12 @@ An "emitter" object must have a function
success: function(data) { success: function(data) {
data = JSON.parse(data); data = JSON.parse(data);
log ("connection.open", ["AJAX/COMET"], data); log ("connection_open", ["AJAX/COMET"], data);
client_hash = data.suid; client_hash = data.suid;
poll(); poll();
}, },
error: function(req, stat, err) { error: function(req, stat, err) {
Evennia.emit("connection.error", ["AJAX/COMET init error"], err); Evennia.emit("connection_error", ["AJAX/COMET init error"], err);
log("AJAX/COMET: Connection error: " + err); log("AJAX/COMET: Connection error: " + err);
} }
}); });
@ -274,7 +270,7 @@ An "emitter" object must have a function
data: {mode:'input', data: JSON.stringify(data), 'suid': client_hash}, data: {mode:'input', data: JSON.stringify(data), 'suid': client_hash},
success: function(req, stat, err) {}, success: function(req, stat, err) {},
error: function(req, stat, err) { error: function(req, stat, err) {
Evennia.emit("connection.error", ["AJAX/COMET send error"], err); Evennia.emit("connection_error", ["AJAX/COMET send error"], err);
log("AJAX/COMET: Server returned error.",req,stat,err); log("AJAX/COMET: Server returned error.",req,stat,err);
} }
}); });
@ -315,11 +311,11 @@ An "emitter" object must have a function
success: function(data){ success: function(data){
client_hash = '0'; client_hash = '0';
Evennia.emit("connection.close", ["AJAX/COMET"], {}); Evennia.emit("connection_close", ["AJAX/COMET"], {});
log("AJAX/COMET connection closed cleanly.") log("AJAX/COMET connection closed cleanly.")
}, },
error: function(req, stat, err){ error: function(req, stat, err){
Evennia.emit("connection.err", ["AJAX/COMET close error"], err); Evennia.emit("connection_err", ["AJAX/COMET close error"], err);
client_hash = '0'; client_hash = '0';
} }
}); });

View file

@ -106,12 +106,15 @@ function onPrompt(args, kwargs) {
"<div id='prompt' class='msg out'>" + args[0] + "</div>"); "<div id='prompt' class='msg out'>" + args[0] + "</div>");
} }
// Handler silencing events we don't do anything with.
function onSilence(cmdname, args, kwargs) {}
// Handler unrecognized commands from server // Handler unrecognized commands from server
function onDefault(cmdname, args, kwargs) { function onDefault(cmdname, args, kwargs) {
mwin = $("#messagewindow"); mwin = $("#messagewindow");
mwin.append( mwin.append(
"<div class='msg err'>" "<div class='msg err'>"
+ "Unhandled event:<br>" + "Error or Unhandled event:<br>"
+ cmdname + ", " + cmdname + ", "
+ JSON.stringify(args) + ", " + JSON.stringify(args) + ", "
+ JSON.stringify(kwargs) + "<p></div>"); + JSON.stringify(kwargs) + "<p></div>");
@ -138,14 +141,17 @@ $(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);
// silence currently unused events
Evennia.emitter.on("connection_open", onSilence);
Evennia.emitter.on("connection_close", onSilence);
// Event when closing window (have to have Evennia initialized) // Event when closing window (have to have Evennia initialized)
$(window).bind("beforeunload", Evennia.connection.close); $(window).bind("beforeunload", Evennia.connection.close);
doWindowResize();
// set an idle timer to send idle every 3 minutes, // set an idle timer to send idle every 3 minutes,
// to avoid proxy servers timing out on us // to avoid proxy servers timing out on us
setInterval(function() { setInterval(function() {
// Connect to server
Evennia.msg("text", ["idle"], {}); Evennia.msg("text", ["idle"], {});
}, },
60000*3 60000*3