Skip to content
Snippets Groups Projects
Commit 9c120b61 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Cleanup on peer disconnect

parent 6cce30b2
No related branches found
No related tags found
2 merge requests!29Feature/wstunnel implements #71 and fixes #61,!27Partial implementation of #71 websockets
......@@ -42,7 +42,22 @@ app.ws('/', (ws, req) => {
});
}
});
})
});
p.on("disconnect", (peer) => {
console.log("DISCONNECT");
// Remove all peer details and streams....
let puris = peer_uris[peer.string_id];
if (puris) {
for (let i=0; i<puris.length; i++) {
console.log("Removing stream: ", puris[i]);
delete uri_to_peer[puris[i]];
}
delete peer_uris[peer.string_id];
}
if (peer_by_id.hasOwnProperty(peer.string_id)) delete peer_by_id[peer.string_id];
});
p.bind("list_streams", () => {
return Object.keys(uri_to_peer);
......@@ -72,7 +87,6 @@ app.ws('/', (ws, req) => {
});
p.bind("get_stream", (uri, N, rate, pid, dest) => {
console.log("GET STREAM: ", uri);
let peer = uri_to_peer[uri];
if (peer) {
peer.bind(uri, (rgb, depth) => {
......
......@@ -52,7 +52,7 @@ function Peer(ws) {
this.sock.on("close", () => {
this.status = kDisconnected;
this._notify("disconnect");
this._notify("disconnect", this);
});
this.sock.on("error", () => {
......@@ -91,11 +91,20 @@ Peer.prototype._dispatchCall = function(name, id, args) {
if (this.bindings.hasOwnProperty(name)) {
console.log("Call for:", name, id);
let res = this.bindings[name].apply(this, args);
try {
this.sock.send(encode([1,id,name,res]));
} catch(e) {
this.close();
}
} else if (this.proxies.hasOwnProperty(name)) {
console.log("Proxy for:", name, id);
args.unshift((res) => {
try {
this.sock.send(encode([1,id,name,res]));
} catch(e) {
this.close();
}
});
this.proxies[name].apply(this, args);
} else {
......@@ -114,7 +123,8 @@ Peer.prototype._dispatchResponse = function(id, res) {
Peer.prototype.bind = function(name, f) {
if (this.bindings.hasOwnProperty(name)) {
console.error("Duplicate bind to same procedure");
//console.error("Duplicate bind to same procedure");
this.bindings[name] = f;
} else {
this.bindings[name] = f;
}
......@@ -122,7 +132,8 @@ Peer.prototype.bind = function(name, f) {
Peer.prototype.proxy = function(name, f) {
if (this.proxies.hasOwnProperty(name)) {
console.error("Duplicate proxy to same procedure");
//console.error("Duplicate proxy to same procedure");
this.proxies[name] = f;
} else {
this.proxies[name] = f;
}
......@@ -131,11 +142,20 @@ Peer.prototype.proxy = function(name, f) {
Peer.prototype.rpc = function(name, cb, ...args) {
let id = this.cbid++;
this.callbacks[id] = cb;
try {
this.sock.send(encode([0, id, name, args]));
} catch(e) {
this.close();
}
}
Peer.prototype.send = function(name, ...args) {
try {
this.sock.send(encode([0, name, args]));
} catch(e) {
this.close();
}
}
Peer.prototype.close = function() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment