doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
721 stars 124 forks source link

The 'Connection' header value 'Upgrade, Upgrade' is invalid. #315

Closed asharmaarcadix closed 1 year ago

asharmaarcadix commented 1 year ago

Hi,

We are having a node.js Server for socket.io and have hosted it to IIS server.

And for client side we are using SocketIOClient package in .net core 6.0. as we try to start a connection we face an issue "TransportException: Could not connect to 'ws://localhost:8000/socket.io/?EIO=4&transport=websocket'" "The 'Connection' header value 'Upgrade, Upgrade' is invalid."

image

image

But when we try to make connection without hosting it to IIS server ( normally in local). It is working fine but after hosting it to IIS Server, we are not able to make connection.

Here is the image from index.js of server side. image

Can you please guide me to resolve this issue.

doghappy commented 1 year ago

thanks for your feedback.

it seems that IIS returned 2 Connection: Upgrade

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Server: Microsoft-IIS/10.0
Connection: Upgrade
Sec-WebSocket-Accept: NFMfylbwIygrHXmqMg4BFjGQITw=
Connection: Upgrade
X-Powered-By: ASP.NET
Date: Fri, 31 Mar 2023 14:58:21 GMT
EndTime: 22:58:21.838
ReceivedBytes: 109
SentBytes: 0

I don't know why there are 2 Connection: Upgrade. the implementation of Microsoft will throw an exception after received that response:

        ValidateHeader(response.Headers, HttpKnownHeaderNames.Connection, "Upgrade");

        private static void ValidateHeader(HttpHeaders headers, string name, string expectedValue)
        {
            if (headers.NonValidated.TryGetValues(name, out HeaderStringValues hsv))
            {
                if (hsv.Count == 1)
                {
                    foreach (string value in hsv)
                    {
                        if (string.Equals(value, expectedValue, StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }
                        break;
                    }
                }

                throw new WebSocketException(WebSocketError.HeaderError, SR.Format(SR.net_WebSockets_InvalidResponseHeader, name, hsv));
            }

            throw new WebSocketException(WebSocketError.Faulted, SR.Format(SR.net_WebSockets_MissingResponseHeader, name));
        }

I haven't found root cause yet. so let's use HttpPooling instead of WebSocket

var io = new SocketIO(SocketIOOptions
{
    AutoUpgrade = false,
});