Archived
1
0

unscrewup reconnect

This commit is contained in:
Jeff Becker 2016-07-05 20:43:36 -04:00
parent a0bc39aaa5
commit b2c1d9eb26
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B

View File

@ -448,9 +448,23 @@ function buildChat(chat, domElem, channel) {
}; };
} }
function Connection(ws, url) { function Connection(chat, ws, url) {
this.ws = ws; this.ws = ws;
this.chat = chat;
this.url = url; this.url = url;
this.ws.onmessage = function(ev) {
chat.handleData(JSON.parse(ev.data));
}
this.ws.onclose = function() {
self.ws = null;
}
var self = this;
setInterval(function() {
if (self.ws == null) {
// try reconnecting
initWebSocket(self.chat, self.url, self);
}
}, 5000);
} }
Connection.prototype.ban = function(reason) { Connection.prototype.ban = function(reason) {
@ -472,15 +486,6 @@ Connection.prototype.sendBinary = function(obj) {
} }
} }
Connection.prototype.onmessage = function(callback) {
if (this.ws) {
this.ws.onmessage = function(event) {
var data = JSON.parse(event.data);
callback(data);
}
}
}
Connection.prototype.onclose = function(callback) { Connection.prototype.onclose = function(callback) {
if(this.ws) if(this.ws)
this.ws.onclose = callback; this.ws.onclose = callback;
@ -491,28 +496,31 @@ Connection.prototype.onclose = function(callback) {
* @param channel The channel to open a connection to. * @param channel The channel to open a connection to.
* @return A connection the the websocket. * @return A connection the the websocket.
*/ */
function initWebSocket(url, connection) { function initWebSocket(chat, url, connection) {
var ws = null; var ws = null;
if (window['WebSocket']) { if (window['WebSocket']) {
try { try {
var w = new WebSocket(url); ws = new WebSocket(url);
ws = w;
} catch (ex) { } catch (ex) {
ws = null; ws = null;
} }
} }
if (ws !== null) { if (ws) {
ws.onclose = function() { ws.onclose = function() {
if (connection) { if (connection) {
connection.ws = null; connection.ws = null;
} }
}; };
if (connection) { if (connection) {
console.log("reconnected.");
connection.ws = ws; connection.ws = ws;
connection.ws.onmessage = function(ev) {
chat.handleData(JSON.parse(ev.data));
}
chat.clear();
chat.handleMessage({Type: "post", PostMessage: "reconnecting..."});
return connection; return connection;
} else { } else {
return new Connection(ws, url); return new Connection(chat, ws, url);
} }
} }
} }
@ -670,7 +678,8 @@ ConvoBar.prototype.removeConvo = function(msgid) {
var e = document.getElementById("livechan_convobar_item_"+c.id); var e = document.getElementById("livechan_convobar_item_"+c.id);
if (e) e.remove(); if (e) e.remove();
for(var idx = 0; idx < c.posts.length; idx ++ ) { for(var idx = 0; idx < c.posts.length; idx ++ ) {
var child = document.getElementById("livechan_chat_"+c.posts[idx].ShortHash); var id = "livechan_chat_"+c.posts[idx].HashShort;
var child = document.getElementById(id);
if(child) child.remove(); if(child) child.remove();
} }
@ -860,7 +869,7 @@ function Chat(domElem, channel, options) {
var scheme = "wss://"; var scheme = "wss://";
if (location.protocol == "http:") scheme = "ws://"; if (location.protocol == "http:") scheme = "ws://";
var url = scheme + location.host + prefix + "live?"+ this.name; var url = scheme + location.host + prefix + "live?"+ this.name;
this.connection = initWebSocket(url); this.connection = initWebSocket(this, url);
this.initOutput(); this.initOutput();
this.initInput(); this.initInput();
// set navbar channel name // set navbar channel name
@ -878,6 +887,12 @@ function Chat(domElem, channel, options) {
this.login(); this.login();
} }
Chat.prototype.clear = function () {
for ( var convo in this.chatElems.convobar.holder ) {
this.chatElems.convobar.removeConvo(convo);
}
}
/** /**
* @brief begin login sequence * @brief begin login sequence
*/ */
@ -1097,34 +1112,24 @@ Chat.prototype.handleMessage = function (data) {
} }
} }
Chat.prototype.handleData = function(data) {
var self = this;
if( Object.prototype.toString.call(data) === '[object Array]' ) {
for (var i = 0; i < data.length; i++) {
self.handleMessage(data[i]);
}
} else {
self.handleMessage(data);
}
}
/* @brief Binds messages to be displayed to the output. /* @brief Binds messages to be displayed to the output.
*/ */
Chat.prototype.initOutput = function() { Chat.prototype.initOutput = function() {
var outputElem = this.chatElems.output; var outputElem = this.chatElems.output;
var connection = this.connection; var connection = this.connection;
var self = this; var self = this;
connection.onmessage(function(data) {
if( Object.prototype.toString.call(data) === '[object Array]' ) {
for (var i = 0; i < data.length; i++) {
self.handleMessage(data[i]);
}
} else {
self.handleMessage(data);
}
});
var reconnect = function() {
connection.ws = null;
var getConnection = setInterval(function() {
console.log("Attempting to reconnect.");
if (initWebSocket(connection.url, connection) !== null
&& connection.ws !== null) {
setTimeout(function() {
clearInterval(getConnection);
}, 100);
}
}, 5000);
};
connection.onclose(reconnect);
} }
/* @brief update the user counter for number of users online /* @brief update the user counter for number of users online