diff --git a/examples/websockets/node/Makefile b/examples/websockets/node/Makefile new file mode 100644 index 000000000..cde795492 --- /dev/null +++ b/examples/websockets/node/Makefile @@ -0,0 +1,6 @@ +install: + npm install websocket + + +run: + nodejs example-server.js diff --git a/examples/websockets/node/example-server.js b/examples/websockets/node/example-server.js new file mode 100644 index 000000000..c7bac0d44 --- /dev/null +++ b/examples/websockets/node/example-server.js @@ -0,0 +1,57 @@ +"use strict"; + +var serverPort = 8080; + +var websocket = require('websocket').server; +var http = require('http'); + +var server = http.createServer(function(request, response) { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.write('This is a websocket server, not intended for http\n'); + response.end(); +}); + +server.listen(serverPort, function() { + console.log('Server is listening on port ' + serverPort); +}); + +var wsServer = new websocket({ + httpServer: server +}); + + +var connections = []; + +function broadcastMessage(message) { + for (var i = 0; i < connections.length; i++) { + connections[i].sendUTF(message); + } +} + +wsServer.on('request', function(request) { + /* Save the connection */ + var connection = request.accept(null, request.origin); + + /* Store the connection in the list of connections */ + var connectionIndex = connections.push(connection) - 1; + + console.log('Connection from ' + connection.remoteAddress + '.'); + + broadcastMessage('Connection from ' + connection.remoteAddress + '.'); + + connection.on('message', function(message) { + if (message.type === 'utf8') { + console.log((new Date()) + ' Message received: ' + + message.utf8Data); + broadcastMessage(message.utf8Data); + } + }); + + // user disconnected + connection.on('close', function(connection) { + console.log((new Date()) + ' Connection lost: ' + + connection.remoteAddress); + connections.splice(connectionIndex, 1); + }); + +});