Socket.io-client-swift: Invalid HTTP upgrade

Created on 2 Feb 2018  ·  10Comments  ·  Source: socketio/socket.io-client-swift

Hello. I've recently updated to a new version Socket.IO-Client-Swift 13.1.0 form 11.1.3 and noticed that similar code on the old version successfully connects to the socket server but the new updated code below is stuck in a reconnecting loop with the following response in logs:

2018-02-02 16:21:55.929935-0500 TheApp[243:5822] LOG SocketManager: Trying to reconnect
2018-02-02 16:21:55.930611-0500 TheApp[243:5822] LOG SocketIOClient{/}: Handling event: reconnectAttempt with data: [-1]
2018-02-02 16:21:55.931597-0500 TheApp[243:6102] LOG SocketEngine: Starting engine. Server: ws://fw-socket-prod.herokuapp.com
2018-02-02 16:21:55.931887-0500 TheApp[243:6102] LOG SocketEngine: Handshaking
2018-02-02 16:21:57.049287-0500 TheApp[243:6081] ERROR SocketEngine: The operation couldn’t be completed. (Starscream.WSError error 1.)
2018-02-02 16:21:57.049741-0500 TheApp[243:5822] ERROR SocketManager: The operation couldn’t be completed. (Starscream.WSError error 1.)
2018-02-02 16:21:57.050055-0500 TheApp[243:5822] LOG SocketIOClient{/}: Handling event: error with data: ["The operation couldn’t be completed. (Starscream.WSError error 1.)"]
2018-02-02 16:21:59.110895-0500 TheApp[243:5822] LOG SocketManager: Trying to reconnect
2018-02-02 16:21:59.111181-0500 TheApp[243:5822] LOG SocketIOClient{/}: Handling event: reconnectAttempt with data: [-2]

After a short research, I've found that the issue Starscream.WSError error 1 means: "Invalid HTTP upgrade"

The logs on server-side show:

Feb 02 12:39:50 fw-rt-node heroku/router: at=info method=GET path="/socket.io/?transport=websocket" host=fw-rt-node.herokuapp.com request_id=b7e41180-5213-49ee-b317-0ec0d9a697e9 fwd="65.209.36.34" dyno=web.2 connect=2ms service=29052ms status=101 bytes=285 protocol=http

The code:

let ssl = SSLSecurity(usePublicKeys: true)
            let specs: SocketIOClientConfiguration = [.path("/theApp"),
                                                      .forceWebsockets(true),
                                                      .reconnectAttempts(3),
                                                      .reconnectWait(3),
                                                      .security(ssl),
                                                      .log(true)]
            socketManager = SocketManager(socketURL: url, config: specs)

            socket?.on(clientEvent: .connect) {data, emitter in
                // handle connected
            }

            socket?.on(clientEvent: .disconnect, callback: { (data, emitter) in
                //handle diconnect
            })

            socket?.onAny({ (event) in
               //handle event
            })

            socket?.connect()

Most helpful comment

@nuclearace any luck here? we are stuck here.

All 10 comments

Perhaps some sort of TLS issue?

I wish I know. I've pulled back socket.io-client to the latest 12.1.3 version and it works fine for me now.

@maksTheAwesome Have you figured it out? I'm having the same problem

@pablogeek Actually I did not. I rolled socket.io-client back to 12.1.3 and the problem was gone. So I never returned to the issue.

@maksTheAwesome can you share your code how you implement client now?

Having the exact same issue, rolling back to 12.1.3. fixed it for me. I noticed that Starscream went from 3.0.5 to 2.1.1, so problem is probably on that side.

Actually I have experienced the same error.
WS server was under ssl terminating load balancer AND nginx proxy (don't ask me why).
So if request contains Host: :443, ssl termination fails it.
When I changed
let hostValue = request.allHTTPHeaderFields?[headerWSHostName] ?? "(url.host!):(port!)"
to
let hostValue = request.allHTTPHeaderFields?[headerWSHostName] ?? "(url.host!)"
it started working.

Hi, @maksTheAwesome
I am facing on same issue. Now the socket version I am using is v13.2.1
So should I use v12.1.3? then this problem is solved?

@nuclearace any luck here? we are stuck here.

I'm seeing the same issue in iOS 14, seems like a regression somewhere. It works 99% of the time, but sometimes the app gets in a state where it's stuck failing with this error, and the knot solution is to restart it.

Was this page helpful?
0 / 5 - 0 ratings