-
-
Save robertklep/5b299451d8bd450844a8 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
var http = require('http'); | |
var WebSocketServer = require('ws').Server; | |
var wss = new WebSocketServer({ port : 9001 }); | |
http.createServer(function(req, res) { | |
res.end([ | |
'<script>', | |
'var ws = new WebSocket("ws://localhost:9001");', | |
'ws.onmessage = function(ev) {', | |
' console.log("received message: " + ev.data);', | |
'};', | |
'setInterval(function() {', | |
' ws.send(Math.random());', | |
'}, 1000);', | |
'</script>' | |
].join('\n')); | |
}).listen(9000, function() { | |
console.log('Open http://localhost:9000/ in your browser'); | |
}); | |
wss.broadcast = function(data) { | |
for (var i in this.clients) | |
this.clients[i].send(data); | |
}; | |
wss.on('connection', function(ws) { | |
ws.on('message', function(message) { | |
wss.broadcast('Broadcast message: ' + message); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment