Handle WS reconnection in messaging module

This commit is contained in:
Tissevert 2019-11-24 22:56:53 +01:00
parent 11e33a95bb
commit 9e228c7e94

View file

@ -1,15 +1,25 @@
function Messaging(modules) { function Messaging(modules) {
var ws = new WebSocket(window.location.origin.replace(/^http/, 'ws') + '/play/'); var wsLocation = window.location.origin.replace(/^http/, 'ws') + '/play/';
var ws;
var debug = getParameters().debug; var debug = getParameters().debug;
var doLog = debug != undefined && debug.match(/^(?:1|t(?:rue)?|v(?:rai)?)$/i); var doLog = debug != undefined && debug.match(/^(?:1|t(?:rue)?|v(?:rai)?)$/i);
var keepAlivePeriod = 20000; var on = false;
var s = 1000; /* ms */
var keepAlivePeriod = 20;
var reconnectDelay = 1;
var routes = {callbacks: [], children: {}}; var routes = {callbacks: [], children: {}};
var wsHandlers = {
open: [function() {on = true; reconnectDelay = 1}, ping],
close: [function() {on = false;}, reconnect]
};
init();
return { return {
addEventListener: addEventListener, addEventListener: addEventListener,
send: send, isOn: isOn,
start: start send: send
} };
function get(obj, path, write) { function get(obj, path, write) {
write = write || false; write = write || false;
@ -37,8 +47,16 @@ function Messaging(modules) {
} }
function addEventListener(path, callback) { function addEventListener(path, callback) {
var route = get(routes, path, true); if(Array.isArray(path)) {
route.callbacks.push(callback); var route = get(routes, path, true);
route.callbacks.push(callback);
} else {
if(wsHandlers[path] != undefined) {
wsHandlers[path].push(callback);
} else {
log('Unsupported websocket event "' + path + '"');
}
}
} }
function messageListener(event) { function messageListener(event) {
@ -59,26 +77,51 @@ function Messaging(modules) {
log(o); log(o);
}; };
function log(message) {
if(doLog) {
console.log(message);
}
}
function start() {
ws.addEventListener('message', messageListener);
ws.addEventListener('open', ping);
addEventListener(["Pong"], ping);
addEventListener(["Error"], function(o) {modules.screen.error(o.error);});
}
function send(o) { function send(o) {
ws.send(JSON.stringify(o)); ws.send(JSON.stringify(o));
o.direction = 'client > server'; o.direction = 'client > server';
log(o); log(o);
} }
function log(message) {
if(doLog) {
console.log(message);
}
}
function init() {
connect();
addEventListener(["Pong"], ping);
addEventListener(["Error"], function(o) {modules.screen.error(o.error);});
}
function connect() {
ws = new WebSocket(window.location.origin.replace(/^http/, 'ws') + '/play/');
ws.addEventListener('message', messageListener);
ws.addEventListener('open', function(e) {
wsHandlers.open.forEach(function(handler) {handler(e);});
});
ws.addEventListener('close', function(e) {
wsHandlers.close.forEach(function(handler) {handler(e);});
});
}
function reconnect() {
setTimeout(connect, reconnectDelay * s);
if(reconnectDelay < 16) {
reconnectDelay *= 2;
}
}
function isOn() {
return on;
}
function ping() { function ping() {
setTimeout(function() {send({tag: "Ping"});}, keepAlivePeriod); setTimeout(function() {
if(isOn()) {
send({tag: "Ping"});
}
}, keepAlivePeriod * s);
} }
} }