Skip to content

Instantly share code, notes, and snippets.

@grundyoso
Created April 15, 2017 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grundyoso/4aca5ef319b952bea09ca9a3b51113d3 to your computer and use it in GitHub Desktop.
Save grundyoso/4aca5ef319b952bea09ca9a3b51113d3 to your computer and use it in GitHub Desktop.
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