Skip to content

Commit

Permalink
fix: prevent crash with browser ws
Browse files Browse the repository at this point in the history
If the browser's ws is disconnected without this being detected, sending messages caused FGMachine to crash
  • Loading branch information
Kaixhin committed Dec 11, 2015
1 parent a4ad957 commit 8f63147
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,17 @@ if (!process.env.FGMACHINE_URL) {
/* WebSocket server */
// Add websocket server
var wss = new WebSocketServer({server: server});
// Catches errors to prevent FGMachine crashing if browser disconnect undetected
var wsErrHandler = function() {};

// Call on connection from new client
wss.on("connection", function(ws) {
// Listeners
var sendStdout = function(data) {
ws.send(JSON.stringify({stdout: data}));
ws.send(JSON.stringify({stdout: data}), wsErrHandler);
};
var sendStderr = function(data) {
ws.send(JSON.stringify({stderr: data}));
ws.send(JSON.stringify({stderr: data}), wsErrHandler);
};

// Check subscription for logs
Expand Down

0 comments on commit 8f63147

Please sign in to comment.