Skip to content

Instantly share code, notes, and snippets.

@nulltask
Created April 30, 2014 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nulltask/11421778 to your computer and use it in GitHub Desktop.
Save nulltask/11421778 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<body>
<script>
var worker = new Worker('./worker.js');
worker.onmessage = function(e) {
console.log(e.data);
};
</script>
</body>
</html>
var debug = require('debug')('app');
var WebSocket = require('ws');
var WebSocketServer = WebSocket.Server;
var server = new WebSocketServer({ port: 8080 });
server.on('connection', function(ws) {
debug('client is opened');
var timerId = setInterval(function() {
debug('send hello');
try {
ws.send(JSON.stringify({ hello: 'world' }));
} catch (e) {
console.error(e);
clearInterval(timerId);
}
}, 1000);
});
{
"name": "ws-worker",
"dependencies": {
"debug": "^0.8.1",
"ws": "^0.4.31"
}
}
var ws = new WebSocket('ws://localhost:8080');
ws.onmessage = function(e) {
postMessage(JSON.parse(e.data));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment