Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket heartbeat detection #394

Merged
merged 4 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/perspective/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"uglifyjs-webpack-plugin": "^0.4.6",
"underscore": "^1.8.3",
"utf-8-validate": "~4.0.0",
"websocket-heartbeat-js": "^1.0.7",
"worker-loader": "^2.0.0",
"ws": "^6.1.2"
},
Expand Down
16 changes: 16 additions & 0 deletions packages/perspective/src/js/perspective.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,14 @@ class WebSocketHost extends module.exports.Host {
this.REQS = {};
this._wss = new WebSocket.Server({noServer: true, perMessageDeflate: true});
this._wss.on("connection", ws => {
ws.isAlive = true;
ws.id = CLIENT_ID_GEN++;
ws.on("message", msg => {
ws.isAlive = true;
if (msg === "heartbeat") {
ws.send("heartbeat");
return;
}
msg = JSON.parse(msg);
this.REQS[msg.id] = ws;
try {
Expand All @@ -146,6 +152,16 @@ class WebSocketHost extends module.exports.Host {
ws.on("error", console.error);
});

// clear invalid connections
setInterval(() => {
this._wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
return ws.terminate();
}
ws.isAlive = false;
});
}, 30000);

this._server.on(
"upgrade",
function upgrade(request, socket, head) {
Expand Down
11 changes: 9 additions & 2 deletions packages/perspective/src/js/perspective.parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

import WebsocketHeartbeatJs from "websocket-heartbeat-js";
import * as defaults from "./defaults.js";

import {worker} from "./api.js";
Expand Down Expand Up @@ -111,11 +111,18 @@ class WebWorker extends worker {
class WebSocketWorker extends worker {
constructor(url) {
super();
this._ws = new WebSocket(url);
this._ws = new WebsocketHeartbeatJs({
url,
pingTimeout: 15000,
pingMsg: "heartbeat"
});
this._ws.onopen = () => {
this.send({id: -1, cmd: "init"});
};
this._ws.onmessage = msg => {
if (msg.data === "heartbeat") {
return;
}
this._handle({data: JSON.parse(msg.data)});
};
}
Expand Down