websocket example

This commit is contained in:
AJ ONeal 2015-12-17 02:32:43 -08:00
parent a4aaffdf36
commit 7650b09548
1 changed files with 17 additions and 1 deletions

View File

@ -161,9 +161,25 @@ console.log(results.tlsServers);
Note: you don't need to create websockets for the plain ports.
```
```javascript
var WebSocketServer = require('ws').Server;
results.tlsServers.forEach(function (server) {
var wss = new WebSocketServer({ server: server });
wss.on('connection', onConnection);
});
function onConnection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
}
```
## API