dakov
dakov

Reputation: 1139

Websocket server with ssl support (using websocket.io)

I have a simple Websocket server using node.js and websocket.io

var ws = require('websocket.io')
  , server = ws.listen(8000);

server.on('connection', function (socket) {

  console.log("connected");

  socket.on('message', function (message) { 
        on_message_callback(socket, message);
   });

  socket.on('close', function () { 
    on_close_callback(socket);
  });
});

And this is a major part of client:

const HOST = "wss://127.0.0.1:8000/";
var websocket = new WebSocket(HOST);

websocket.onopen  = function(evt)   { ... };
websocket.onclose = function(evt)   { ... },
websocket.onerror = function(evt)   { ... };
websocket.onmessage = function(evt) { ... };

(I've tested it with wss://echo.websocket.org:443 and works as desired)

Works for HTTP pages as desired. Problem is that I need to work under HTTPS pages, too. I not able to "upgrade" my code to make it work. Can anyone help me? I haven't found any tutorial for websocket.io (I want to keep using the same technologies).

I'm also not sure how to handle certificates. I can only generate self-signed. What for this case? When I create them, I have to import them manually to each browser, so they'll allow this communication?

Thank you.

Upvotes: 1

Views: 3479

Answers (1)

dakov
dakov

Reputation: 1139

Finally figured out solution (using Worlize/websocket-node)

const PORT = 8000;
const SUBPROTOCOL = 'sub-protocol';

var WebSocketServer = require('websocket').server;

var https = require('https');
var fs = require('fs');

// Private key and certification (self-signed for now) 
var options = {
  key: fs.readFileSync('cert/server.key'),
  cert: fs.readFileSync('cert/server.crt')
};

// callback function is called only when localhost:8000 is accessed via https protocol
var server = https.createServer(options, function(request, response) {
    // it sends 404 response so browser stops loading, otherwise it keeps loading 
    console.log((new Date()) + ' Received HTTP(S) request for ' + request.url);
    response.writeHead(404);
    response.end();
}); 

// bind server object to listen to PORT number
server.listen(PORT, function() {
    console.log((new Date()) + ' Server is listening on port ' + PORT);
});

wsServer = new WebSocketServer({
    httpServer: server,
    // You should not use autoAcceptConnections for production
    // applications, as it defeats all standard cross-origin protection
    // facilities built into the protocol and the browser.  You should
    // *always* verify the connection's origin and decide whether or not
    // to accept it.
    autoAcceptConnections: false
});


function originIsAllowed(origin) {
  // put logic here to detect whether the specified origin is allowed.
  return true;
}

// If autoAcceptConnections is set to false, a request event will be emitted
// by the server whenever a new WebSocket request is made
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;
    }

    // accepts connection and return socket for this connection
    var connection = request.accept(SUB_PROTOCOL, request.origin);

    console.log((new Date()) + ' Connection accepted.');

    // when message is received
    connection.on('message', function(message) {

        // echo
        connection.send(connection, message.utf8Data);

    });


    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });

});

Upvotes: 1

Related Questions