websocket - 使用一个端口号创建多个websocket服务器

标签 websocket netty

我正在使用 netty 4.0.20 我想使用不同的 url 在同一端口上创建不同的 websocket 服务器

例如, wss://localhost:1234/PathA

wss://localhost:1234/PathB

wss://localhost:1234/PathC

这可能吗?

最佳答案

是的,这可以通过使用反向代理来实现,这可以通过 Nginx 来完成。

这将需要在您的设置中添加一台额外的服务器。

首先,您必须将每个服务器设置为监听不同的端口,然后您需要前端服务器监听所需的公共(public)端口(在您的情况下,这是 1234)。

假设您有以下服务器

  1. Nginx 监听 0.0.0.0:1234
  2. Netty 服务于 /PathA 并监听 0.0.0.0:1235
  3. Netty 服务 /PathB 并监听 0.0.0.0:1236
  4. Netty 服务 /PathC 并监听 0.0.0.0:1237

现在您要做的就是编写一个 Nginx 配置文件,将连接从 HTTP 升级到 Websocket,然后将每个路径反向代理到其相应的服务器。下面是一个可以为您完成这项工作的示例配置文件。

{
    listen 1234;
    server_name localhost;

    location ~PathA/$ {
        proxy_pass http://localhost:1235;
        proxy_http_version 1.1;
        proxy_set_header Upgrade "websocket";
        proxy_set_header Connection "upgrade";
    }

    location ~PathB/$ {
        proxy_pass http://localhost:1236;
        proxy_http_version 1.1;
        proxy_set_header Upgrade "websocket";
        proxy_set_header Connection "upgrade";
    }

    location ~PathC/$ {
        proxy_pass http://localhost:1237;
        proxy_http_version 1.1;
        proxy_set_header Upgrade "websocket";
        proxy_set_header Connection "upgrade";
    }
}

关于websocket - 使用一个端口号创建多个websocket服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781886/

相关文章:

java - Netty ChannelAcquired 没有被调用

java - 在非 Netty 客户端中获取 Netty 服务器的响应

url - 这个 websocket url "ws://{{$}}/ws"是什么意思?

ruby - EventMachine WebSockets - 订阅 EM channel 与保持套接字在集合中

javascript - 无法连接到 API : Should I use websocket or something else?

java - 限制Oio Netty服务器的线程数

python-3.x - Bittrex 网络套接字 API : how to get the order history?

node.js - 是否有不需要使用浏览器的 Node.js 无浏览器 websocket 客户端?

ssl - netty 3.6.0 ssl 超时示例

java - 从服务器向客户端发送消息