【问题标题】:C# WPF Websocket - Cannot established handshake to NGINXC# WPF Websocket - 无法与 NGINX 建立握手
【发布时间】:2021-08-10 22:03:28
【问题描述】:

一直在使用这个客户端库:[Websocket Sharp][1] 为 WPF 应用程序实现我的 websocket 客户端。 问题是,如果我使用这个库的 SetProxy 方法连接没有基本身份验证的 NGINX,即使 NGINX 服务器没有配置基本身份验证,我也无法建立连接。

但是当我将客户端直接连接到后端服务器而不通过 NGINX 时,一切正常。

这是我的客户端代码:

 using (var ws = new WebSocket("ws://localhost:5555/testnginx"))
            {
                ws.SetProxy("http://localhost:5555", null, null);
                ws.OnMessage += (sender, e) => {
                    Debug.WriteLine("Received a message: " + e.Data);
                };

这是我的 NGINX 设置:

location /testnginx{
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header HOST $host;
            proxy_set_header X_Forwarded_For $remote_addr;
            proxy_pass http://socketservers;
            #proxy_pass http://localhost:7777/gs-guide-websocket;
            proxy_redirect default;
            client_max_body_size 1000m; 
        } 

或者有什么替代方法可以连接到支持 NGINX 的 spring websocket 服务器? [1]:https://github.com/sta/websocket-sharp

【问题讨论】:

    标签: c# nginx spring-websocket websocket-sharp


    【解决方案1】:

    解决了这个问题。

    只需要确保NGINX中的路由和stomp中配置的endpoint必须完全一致。

    例如

    NGINX 路线:

    location /testnginx{
    }
    

    Spring stomp websocket 端点:

    @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/testnginx").setAllowedOrigins("*").withSockJS();
        }
    

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多