Created
April 30, 2014 09:04
-
-
Save nulltask/11421778 to your computer and use it in GitHub Desktop.
This file contains 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
<!doctype html> | |
<html> | |
<body> | |
<script> | |
var worker = new Worker('./worker.js'); | |
worker.onmessage = function(e) { | |
console.log(e.data); | |
}; | |
</script> | |
</body> | |
</html> |
This file contains 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
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); | |
}); |
This file contains 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
{ | |
"name": "ws-worker", | |
"dependencies": { | |
"debug": "^0.8.1", | |
"ws": "^0.4.31" | |
} | |
} |
This file contains 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
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