nodejs-websocket
nodejs-websocket copied to clipboard
Run paired with a Node http/https server?
Is there a way to hand ws an http.createServer()
/ https.createServer()
instance, so that a server can handle (limited) HTTP traffic in addition to upgrading a connection to a websocket connection on a specific route?
For instance:
const https = require('https');
const ws = require('node-websocket');
const routes = require('./routes.js');
const server = https.createServer(routes);
ws.setUpgradePath(server, 'join', connection => {
connection.on( ... )
...
});
server.listen(80);
So we have a simple secure http server, with a route that clients can use when creating websockets in order to set up the two way persistent connection, while also being able to load up (for example) a lobby page on the /
route that gives them information on who else is already connected, a button that starts their own websocket client code, etc.
Especially for debugging and administration, being able to generate a webpage that shows the current server state on the same address and port (but not path), rather than using a separate server entirely, is fairly crucial.