Created
June 17, 2016 07:19
-
-
Save kuy/99aff6b53db80ceb1a73a86f2b50326e to your computer and use it in GitHub Desktop.
Remote console.log using WebSocket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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