【问题标题】:Websocket server with ssl support (using websocket.io)支持 ssl 的 Websocket 服务器(使用 websocket.io)
【发布时间】:2014-10-10 13:06:49
【问题描述】:

我有一个使用 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 的任何教程(我想继续使用相同的技术)。

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

谢谢。

【问题讨论】:

  • 你需要使用 wss:// 协议而不是仅仅使用 ws://,但我想你已经知道了,那又是什么问题呢?
  • 是的,当然,我使用“ws”代码作为我所拥有的示例。我需要 - 并且我自己无法弄清楚 - 是如何在我的服务器中实现安全的 wss 协议(使用 websocket.io - 或其他任何东西,这样我就不必更改客户端的代码)
  • @dakov - 正如 Dan 所说,您需要使用 wss。您应该提供您实际使用的代码,而不是错误的示例。
  • 把一个字母改成'wss://...',现在是一模一样的代码(当然实现了回调函数)

标签: javascript node.js ssl websocket certificate


【解决方案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.');
    });

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2016-07-20
    • 2020-07-23
    相关资源
    最近更新 更多