websocket example
This commit is contained in:
parent
a4aaffdf36
commit
7650b09548
18
README.md
18
README.md
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue