61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
/**
|
|
* A messaging class for handling websocket messages and taking the
|
|
* proper web-ui actions. Anything more generic related to message
|
|
* definitions, etc. should be in other places not tied to our UI.
|
|
*/
|
|
(function(context) {
|
|
|
|
context.JK = context.JK || {};
|
|
context.JK.Messaging = function(app) {
|
|
|
|
if ("undefined" === typeof(context.JK.JamServer))
|
|
return;
|
|
|
|
// Alias some of the globals for less typing.
|
|
var logger = context.JK.logger;
|
|
var server = context.JK.JamServer;
|
|
var messages = context.JK.MessageType;
|
|
var msg_factory = context.JK.MessageFactory;
|
|
|
|
var myClientId = null;
|
|
var myPingTimer = null;
|
|
var pingCount = 0;
|
|
var maxPings = 5;
|
|
|
|
function logMessage(header, payload) {
|
|
logger.debug(header.type + ": " + JSON.stringify(payload));
|
|
}
|
|
|
|
function pingMyself() {
|
|
if (!myClientId)
|
|
return;
|
|
pingCount++;
|
|
message = msg_factory.ping(myClientId);
|
|
server.send(message);
|
|
if (pingCount > maxPings) {
|
|
context.clearInterval(myPingTimer);
|
|
myPingTimer = null;
|
|
}
|
|
}
|
|
|
|
function loggedIn(header, payload) {
|
|
logger.debug('Logged In handler: ' + header.type + ':' + JSON.stringify(payload));
|
|
myClientId = payload.client_id;
|
|
myPingTimer = context.setInterval(pingMyself, 1000);
|
|
}
|
|
|
|
function registerLoginPinger() {
|
|
logger.debug("registering login -> pinger");
|
|
server.registerMessageCallback(messages.LOGIN_ACK, loggedIn);
|
|
}
|
|
|
|
/**
|
|
* Register a simple console logger for all known message types.
|
|
*/
|
|
this.register = function() {
|
|
//registerLoginPinger();
|
|
};
|
|
|
|
};
|
|
|
|
})(window); |