made it so websocket connections can't replace each other
This commit is contained in:
parent
09e2d5ba35
commit
78e9ccd60e
38
wstunneld.js
38
wstunneld.js
|
@ -6,33 +6,21 @@ var jwt = require('jsonwebtoken');
|
||||||
var packer = require('tunnel-packer');
|
var packer = require('tunnel-packer');
|
||||||
|
|
||||||
var Devices = {};
|
var Devices = {};
|
||||||
Devices.replace = function (store, servername, newDevice) {
|
Devices.add = function (store, servername, newDevice) {
|
||||||
var devices = Devices.list(store, servername);
|
var devices = Devices.list(store, servername);
|
||||||
var oldDevice;
|
|
||||||
if (!devices.some(function (device, i) {
|
|
||||||
if ((device.deviceId && device.deviceId === newDevice.deviceId)
|
|
||||||
|| (device.servername && device.servername === newDevice.servername)) {
|
|
||||||
oldDevice = devices[i];
|
|
||||||
devices[i] = newDevice;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
})) {
|
|
||||||
devices.push(newDevice);
|
devices.push(newDevice);
|
||||||
store[servername] = devices;
|
store[servername] = devices;
|
||||||
}
|
|
||||||
return oldDevice;
|
|
||||||
};
|
};
|
||||||
Devices.remove = function (store, servername, newDevice) {
|
Devices.remove = function (store, servername, device) {
|
||||||
var devices = Devices.list(store, servername);
|
var devices = Devices.list(store, servername);
|
||||||
var oldDevice;
|
var index = devices.indexOf(device);
|
||||||
devices.some(function (device, i) {
|
|
||||||
if ((device.deviceId && device.deviceId === newDevice.deviceId)
|
if (index < 0) {
|
||||||
|| (device.servername && device.servername === newDevice.servername)) {
|
var id = device.deviceId || device.servername || device.id;
|
||||||
oldDevice = devices.splice(i, 1);
|
console.warn('attempted to remove non-present device', id, 'from', servername);
|
||||||
return true;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
return devices.splice(index, 1)[0];
|
||||||
return oldDevice;
|
|
||||||
};
|
};
|
||||||
Devices.list = function (store, servername) {
|
Devices.list = function (store, servername) {
|
||||||
return store[servername] || [];
|
return store[servername] || [];
|
||||||
|
@ -168,14 +156,10 @@ module.exports.create = function (copts) {
|
||||||
remote.unpacker = packer.create(handlers);
|
remote.unpacker = packer.create(handlers);
|
||||||
|
|
||||||
// Now that we have created our remote object we need to store it in the deviceList for
|
// Now that we have created our remote object we need to store it in the deviceList for
|
||||||
// each domainname we are supposed to be handling. If any previously existing remotes
|
// each domainname we are supposed to be handling.
|
||||||
// have the same identifiers then we replace that remote and close its websocket.
|
|
||||||
token.domains.forEach(function (domainname) {
|
token.domains.forEach(function (domainname) {
|
||||||
console.log('domainname', domainname);
|
console.log('domainname', domainname);
|
||||||
var prev = Devices.replace(deviceLists, domainname, remote);
|
Devices.add(deviceLists, domainname, remote);
|
||||||
if (prev) {
|
|
||||||
prev.ws.close(1001, 'connection replaced');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function forwardMessage(chunk) {
|
function forwardMessage(chunk) {
|
||||||
|
|
Loading…
Reference in New Issue