Created
April 15, 2017 12:02
-
-
Save grundyoso/4aca5ef319b952bea09ca9a3b51113d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const http = require('http'); | |
const url = require('url'); | |
const WebSocket = require('ws'); | |
const app = express(); | |
app.use(function (req, res) { | |
res.send({ msg: "hello" }); | |
}); | |
const server = http.createServer(app); | |
const wss = new WebSocket.Server({ server }); | |
wss.on('connection', function connection(ws) { | |
const 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'); | |
}); | |
server.listen(8010, '0.0.0.0', function listening() { | |
console.log('Listening on %d', server.address().port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment