C# WPF Websocket - Cannot established handshake to NGINX

322 Views Asked by At

Been using this client library: [Websocket Sharp][1] to implement my websocket client for WPF application. The problem is, if I used the SetProxy method of this library to connect with NGINX with no basic auth, I cannot establish a connection even if the NGINX server doesn't have a basic auth configured.

But all is working fine when I connect the client directly to backend server witout passing NGINX.

Here is my client code:

 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);
                };

And here is my NGINX setting:

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; 
        } 

Or is there any alternative to connect to spring websocket server that will support NGINX? [1]: https://github.com/sta/websocket-sharp

1

There are 1 best solutions below

0
On BEST ANSWER

Solved the issue.

Just need to make sure that the route in the NGINX and endpoint configured in stomp must be exactly the same.

For example

NGINX Route:

location /testnginx{
}

Spring stomp websocket endpoint:

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