Integration of web sockets with standard http.Server

I have a simple web server like

var http = require('http'), url = require('url'); var server = http.createServer(function (req, res) { var uri = url.parse(req.url).pathname; if (uri == '/') { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); return; } res.writeHead(404, {'Content-Type': 'text/plain'}); res.end('File not found'); }); server.listen(0, '127.0.0.1'); 

Now I would like to connect one single path (say /ws ) to the websocket, but the rest of the server is unmodified.

However, ws , it seems to me, requires me to configure a dedicated server (with everything that entails, for example, SSL termination, security configuration, and port allocation).

How can I end only the /ws path to the websocket implementation?

+8
websocket
source share
2 answers

I would recommend using the websocket package, this

The Websocket client and server library that implements the WebSocket protocol, as specified in RFC 6455.

This is what I wanted, so I use it. This is incredibly painless, and I actually do both wss and ws connections to node.js from html clients using pure javascript websites.

The git project is active, when I posted the problems, I had the answer the same day. An example from the link above shows how simple it is:

 var WebSocketServer = require('websocket').server; var http = require('http'); var server = http.createServer(function(request, response) { console.log((new Date()) + ' Received request for ' + request.url); response.writeHead(404); response.end(); }); server.listen(8080, function() { console.log((new Date()) + ' Server is listening on port 8080'); }); wsServer = new WebSocketServer({ httpServer: server, autoAcceptConnections: false }); function originIsAllowed(origin) { // put logic here to detect whether the specified origin is allowed. return true; } wsServer.on('request', function(request) { if (!originIsAllowed(request.origin)) { // Make sure we only accept requests from an allowed origin request.reject(); console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); return; } var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted.'); connection.on('message', function(message) { if (message.type === 'utf8') { console.log('Received Message: ' + message.utf8Data); connection.sendUTF(message.utf8Data); } else if (message.type === 'binary') { console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); connection.sendBytes(message.binaryData); } }); connection.on('close', function(reasonCode, description) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); }); 

However, it should be noted that the routines are not obvious, as there are no examples. There are a few questions on the git website for a project that leads you in the right direction.

+9
source

You can add socket.io on the same port with your application. If you want the / ws path, use:

 var io = require('socket.io').listen(httpServer, { resource: '/ws/socket.io' }); 

Or /ws specify /ws/socket.io

+1
source

All Articles