From 7650b095481906ab6f680179cd8e011894c09f7c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 17 Dec 2015 02:32:43 -0800 Subject: [PATCH] websocket example --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d3518b..509824d 100644 --- a/README.md +++ b/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