mirror of https://github.com/Freeboard/plugins.git
A websocket per datasource avoids Socket.io re-connect issues.
parent
20e5a67152
commit
e976bb4525
|
@ -10,38 +10,14 @@
|
|||
|
||||
(function() {
|
||||
|
||||
var connectionCounter = 0;
|
||||
|
||||
var nodeJSDatasource = function(settings, updateCallback) {
|
||||
|
||||
var self = this,
|
||||
connectionId = 0,
|
||||
currentSettings = settings,
|
||||
url,
|
||||
socket,
|
||||
newMessageCallback;
|
||||
|
||||
function setConnectionDead() {
|
||||
if (self.socket && self.socket.connectionsAlive) {
|
||||
if (self.socket.connectionsAlive.indexOf(self.connectionId)>-1){
|
||||
self.socket.connectionsAlive.splice(self.socket.connectionsAlive.indexOf(self.connectionId), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setConnectionAlive() {
|
||||
if (self.socket) {
|
||||
// Initialization
|
||||
if (!self.socket.connectionsAlive) {
|
||||
self.socket.connectionsAlive = [];
|
||||
}
|
||||
// Check if alive connection have been saved
|
||||
if (!_.contains(self.socket.connectionsAlive, self.connectionId)) {
|
||||
self.socket.connectionsAlive.push(self.connectionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onNewMessageHandler(message) {
|
||||
var objdata = JSON.parse(message);
|
||||
if (typeof objdata == "object") {
|
||||
|
@ -49,7 +25,6 @@
|
|||
} else {
|
||||
updateCallback(data);
|
||||
}
|
||||
setConnectionAlive();
|
||||
}
|
||||
|
||||
function joinRoom(roomName, roomEvent) {
|
||||
|
@ -59,30 +34,17 @@
|
|||
console.info("Joining room '%s' with event '%s'", roomName, roomEvent);
|
||||
}
|
||||
|
||||
function checkDiscardSocket() {
|
||||
// Disconnect not active socket
|
||||
function discardSocket() {
|
||||
// Disconnect datasource websocket
|
||||
if (self.socket) {
|
||||
// Discard socket if no other connection are using it
|
||||
if (self.socket.connectionsAlive.length==0 && self.socket.connected) {
|
||||
self.socket.disconnect();
|
||||
console.info("Disconnected from Node.js server at: %s", self.url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function connectToServer(url, rooms) {
|
||||
// Establish connection with server
|
||||
self.url = url;
|
||||
self.socket = io.connect(self.url);
|
||||
|
||||
// Re-connect old disconnect socket
|
||||
if (self.socket) {
|
||||
if (self.socket.connectionsAlive) {
|
||||
if (self.socket.disconnected) {
|
||||
self.socket = io.connect(self.url,{'forceNew':true});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Join the rooms
|
||||
self.socket.on('connect', function() {
|
||||
|
@ -110,8 +72,7 @@
|
|||
|
||||
self.socket.on('reconnect_failed', function(object) {
|
||||
console.error("Re-connection to Node.js failed at: %s", self.url);
|
||||
setConnectionDead();
|
||||
checkDiscardSocket();
|
||||
discardSocket();
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -119,7 +80,7 @@
|
|||
|
||||
function initializeDataSource() {
|
||||
// Reset connection to server
|
||||
checkDiscardSocket();
|
||||
discardSocket();
|
||||
connectToServer(currentSettings.url, currentSettings.rooms);
|
||||
|
||||
// Subscribe to the events
|
||||
|
@ -144,9 +105,7 @@
|
|||
self.newMessageCallback = function(message) {
|
||||
return;
|
||||
};
|
||||
// Set connection has dead
|
||||
setConnectionDead();
|
||||
checkDiscardSocket();
|
||||
discardSocket();
|
||||
};
|
||||
|
||||
this.onSettingsChanged = function(newSettings) {
|
||||
|
@ -154,8 +113,6 @@
|
|||
initializeDataSource();
|
||||
};
|
||||
|
||||
// Main
|
||||
self.connectionId = connectionCounter++;
|
||||
initializeDataSource();
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue