支持ssl的Websocket服务器(使用websocket.io)

时间:2014-08-17 13:02:25

标签: javascript node.js ssl websocket certificate

我有一个使用node.jswebsocket.io

的简单Websocket服务器
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);
  });
});

这是客户的主要部分:

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) { ... };

(我已经使用wss://echo.websocket.org:443对其进行了测试,并按照需要运行)

根据需要适用于HTTP页面。问题是我也需要在HTTPS页面下工作。我无法升级"我的代码使它工作。谁能帮我?我还没找到websocket.io的任何教程(我想继续使用相同的技术)。

我也不确定如何处理证书。我只能生成自签名。这个案子怎么样?当我创建它们时,我必须手动将它们导入到每个浏览器中,以便它们允许这种通信吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

最后找出解决方案(使用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.');
    });

});