Skip to content

Instantly share code, notes, and snippets.

@kuy
Created June 17, 2016 07:19
Show Gist options
  • Select an option

  • Save kuy/99aff6b53db80ceb1a73a86f2b50326e to your computer and use it in GitHub Desktop.

Select an option

Save kuy/99aff6b53db80ceb1a73a86f2b50326e to your computer and use it in GitHub Desktop.
Remote console.log using WebSocket
'use strict';
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 5013 });
wss.on('connection', function connection(ws) {
ws.on('message', function onMessage(message) {
console.log(message);
});
});
'use strict';
var WebSocket = require('ws');
var ws, tmp;
var queue = [];
function flush() {
queue.forEach(function flushEach(msg) {
ws.send(msg);
});
queue = [];
}
module.exports = {
log: function log(message) {
if (typeof ws === 'undefined') {
queue.push(message);
if (typeof tmp === 'undefined') {
tmp = new WebSocket('ws://localhost:5013');
tmp.on('open', function onOpen() {
ws = tmp;
tmp = undefined;
flush();
});
}
} else {
ws.send(message);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment