【问题标题】:Socket.io-based app running through node proxy server disconnecting all sockets whenever one disconnects通过节点代理服务器运行的基于 Socket.io 的应用程序在断开连接时断开所有套接字
【发布时间】:2015-04-22 03:18:00
【问题描述】:

我使用node.jsexpresssocket.io 制作了一个基本的聊天应用程序。它与 socket.io 的tutorial chat app 没有太大区别,它只是在连接的客户端之间发出事件。当我在服务器的 3001 端口上运行它时,它运行良好。

然后我使用node-http-proxy 制作了一个代理服务器应用程序,它侦听端口 80 并根据请求的 url 将流量重定向到我在不同端口上运行的各种独立节点应用程序。很简单。但有些东西正在破裂。每当有人断开连接时,每个套接字都会断开连接并重新连接。这对我的聊天应用很不利,因为它有基于连接的事件。客户端控制台全部显示:

与“ws://[some socket info]”的 WebSocket 连接失败:在收到握手响应之前连接已关闭

这是我认为我的代码的重要部分。

代理服务器.js

var http = require('http');
var httpProxy = require('http-proxy');

//create proxy template object with websockets enabled
var proxy = httpProxy.createProxyServer({ws: true});

//check the header on request and return the appropriate port to proxy to
function sites (req) {
    //webapps get their own dedicated port
    if (req == 'mychatwebsite.com') {return 'http://localhost:3001';}
    else if (req == 'someothersite.com') {return 'http://localhost:3002';}
    //static sites are handled by a vhost server on port 3000
    else {return 'http://localhost:3000';}
}

//create node server on port 80 and proxy to ports accordingly
http.createServer(function (req, res) {
    proxy.web(req, res, { target: sites(req.headers.host) });
}).listen(80);

聊天应用程序.js

/*
...other modules
*/
var express = require("express");
var app = exports.app = express(); //I probably don't need "exports.app" anymore
var http = require("http").Server(app);
var io = require("socket.io")(http);

io.on("connection", function (socket) {
  /*
  ...fun socket.on and io.emit stuff
  */
  socket.on("disconnect", function () {
    //say bye
  });
});

http.listen(3001, function () {
  console.log("listening on port 3001");
});

现在从我在socket.io's site 上读到的内容来看,我可能需要使用一些东西来通过我的代理服务器传输套接字流量。我认为 node-http-proxy 使用{ws: true} 选项为我做到了这一点,正如their docs 中所述,但显然它不像我想象的那样工作。 socket.io 提到了三个不同的东西:

我完全不知道这意味着什么。我在这里的编码不小心超出了我的技能水平,我不知道这些工具中的哪一个可以解决我的问题(如果有的话),甚至我的问题的真正原因是什么。

强制道歉:我是node.js的新手,请见谅。

另外:我知道像 nginx 这样的其他应用程序可以解决我的很多问题,但我的目标是在开始使用新工具之前学习和了解如何使用这套工具。而且,我使用的应用程序越少越好。

【问题讨论】:

    标签: node.js sockets socket.io node-http-proxy


    【解决方案1】:

    我认为您对需要“通过代理服务器传输套接字流量”的直觉是正确的。为了建立 websocket,客户端发出一个带有特殊 Upgrade 标头的 HTTP 请求,通知服务器切换协议 (RFC 6455)。在 node 中,http.Server 实例在发生这种情况时会发出 upgrade 事件,如果未处理该事件,则立即关闭连接。

    你需要在你的http服务器上监听upgrade事件并处理它:

    var proxy = httpProxy.createProxyServer({ws: true})
    var http = http.createServer(/* snip */).listen(80)
    
    // handle upgrade events by proxying websockets
    // something like this
    http.on('upgrade', function (req, socket, head) {
      proxy.ws(req, socket, head, {target:sites(req.headers.host)})
    })
    

    请参阅node docs on the upgrade eventnode-http-proxy docs 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 2015-08-14
      • 2014-12-02
      • 1970-01-01
      相关资源
      最近更新 更多