From 0513c4287aeadd74ed8f78feffc5242cf9c9589b Mon Sep 17 00:00:00 2001 From: Asad Ali Date: Tue, 29 Nov 2022 03:39:29 -0800 Subject: [PATCH 1/2] Multiple servers sharing data with each other --- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f34c7e1..b50cda63 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,86 @@ client.onmessage = function(e) { } }; ``` - +Multiple servers sharing data with each other using the *W3C WebSocket API* +-------------------------------------------------------------------------- +This is a simple example multiple clients will automatically connect to the server and will able to sharing data with each other in realtime. +Example using the [W3C WebSocket API](http://www.w3.org/TR/websockets/). + +```javascript +var WebSocketServer = require('websocket').server; +var http = require('http'); + +var server = http.createServer(function (request, response) { + console.log((new Date()) + ' Received request for ' + request.url); + response.writeHead(404); + response.end(); +}); +server.listen(8000, function () { + console.log((new Date()) + ' Server is listening on port 8080'); +}); + + +const getUniqueID = () => { + const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); + return s4() + s4() + '-' + s4(); +}; + +wsServer = new WebSocketServer({ + httpServer: server, + // You should not use autoAcceptConnections for production + // applications, as it defeats all standard cross-origin protection + // facilities built into the protocol and the browser. You should + // *always* verify the connection's origin and decide whether or not + // to accept it. + autoAcceptConnections: false +}); + +function originIsAllowed(origin) { + // put logic here to detect whether the specified origin is allowed. + return true; +} + +var clients = {}; + +wsServer.on('request', function (request) { + var userID = getUniqueID(); + + if (!originIsAllowed(request.origin)) { + // Make sure we only accept requests from an allowed origin + request.reject(); + console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); + return; + } + + var connection = request.accept('echo-protocol', request.origin); + clients[userID] = connection; + + console.log('connected: ' + userID + ' in ' + Object.getOwnPropertyNames(clients)); + connection.on('message', function (message) { + if (message.type === 'utf8') { + + for (key in clients) { + if (clients[key] != clients[userID]) { + clients[key].sendUTF(message.utf8Data); + console.log('sent Message to: ', clients[key]); + } + } + } + else if (message.type === 'binary') { + for (key in clients) { + console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); + connection.sendBytes(message.binaryData); + } + } + }); + connection.on('close', function (reasonCode, description) { + console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); + }); +}); +``` + +[Github Repo](http://www.w3.org/TR/websockets/). + Request Router Example ---------------------- @@ -252,4 +331,5 @@ For an example of using the request router, see `libwebsockets-test-server.js` i Resources --------- + A presentation on the state of the WebSockets protocol that I gave on July 23, 2011 at the LA Hacker News meetup. [WebSockets: The Real-Time Web, Delivered](http://www.scribd.com/doc/60898569/WebSockets-The-Real-Time-Web-Delivered) From 46857c7f22acf17fccc9e8c2f995c3be812f6fbc Mon Sep 17 00:00:00 2001 From: Asad Ali Date: Tue, 29 Nov 2022 05:16:22 -0800 Subject: [PATCH 2/2] github repo added --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b50cda63..3c8b2459 100644 --- a/README.md +++ b/README.md @@ -320,7 +320,7 @@ wsServer.on('request', function (request) { }); ``` -[Github Repo](http://www.w3.org/TR/websockets/). +[Github Repo](https://github.com/asadalibest1/web-sockets-with-react). Request Router Example ----------------------