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

Man*_*nos 3 websocket netty

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

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

wss://localhost:1234/PathB

wss://localhost:1234/PathC

那可能吗?

Par*_*ris 5

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

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

首先,您必须将每个服务器设置为侦听不同的端口,然后您需要前端服务器侦听所需的公共端口(在您的情况下,这是 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";
    }
}
Run Code Online (Sandbox Code Playgroud)