Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated readme with a new section #443

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](https://github.com/asadalibest1/web-sockets-with-react).

Request Router Example
----------------------

Expand All @@ -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)