socketio / socket.io-client-swift

Other
5.22k stars 842 forks source link

Connection always fails with unspecified "Error" #1333

Closed michaellie99 closed 3 years ago

michaellie99 commented 3 years ago

The socket connection seems to always fail, the connection seems to be found but fails to connect which led me to believe that something else is wrong in my code. Here is the class that I made to handle the socket.io request:

class WebSocketService{

var manager = SocketManager(socketURL: URL(string: "http://192.168.0.140:3000")!, config: [.log(true), .reconnects(false)])

var socket:SocketIOClient!

init(){
    socket = manager.defaultSocket
    setup()
}
func setup(){
    socket.on(clientEvent: .connect){ (data,ack) in
        print("Socket Connected")
    }
    socket.on(clientEvent: .disconnect){ (data,ack) in
        print("Socket Disconnect")
    }
    socket.on(clientEvent: .reconnect){ (data,ack) in
        print("Event: Trying to reconnect \(data)")
    }
    socket.on(clientEvent: .ping){ (data,ack) in
        print("Event: Socket pinged\(data)")
    }
    socket.on(clientEvent: .error){ (data,ack) in
        print("Event: Error, ERROR Occured \(data)")
    }
    socket.on(clientEvent: .reconnectAttempt){ (data,ack) in
        print("Event: Reconnection Attempt Occured \(data)")
    }
    socket.on(clientEvent: .pong){ (data,ack) in
        print("Event: Socket ponged\(data)")
    }

}
func stop(){
    socket.disconnect()
}
func start(){
    socket.connect()
}
func sendMsg(){
    socket.emit("Server", "Hello")
}
}

Here are the logs

2021-03-02 15:17:18.260226+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: connect 2021-03-02 15:17:18.260354+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: broadcast 2021-03-02 15:17:18.260417+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: Mobile 2021-03-02 15:17:18.260476+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: disconnect 2021-03-02 15:17:18.260530+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: reconnect 2021-03-02 15:17:18.260581+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: ping 2021-03-02 15:17:18.260625+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: error 2021-03-02 15:17:18.260665+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: reconnectAttempt 2021-03-02 15:17:18.260757+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Adding handler for event: pong 2021-03-02 15:17:46.523091+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] 2021-03-02 15:17:46.523876+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Joining namespace / 2021-03-02 15:17:46.524466+0700 WebsocketTest[1641:1367041] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting 2021-03-02 15:17:46.524831+0700 WebsocketTest[1641:1367041] LOG SocketManager: Adding engine 2021-03-02 15:17:46.528857+0700 WebsocketTest[1641:1367063] LOG SocketEngine: Starting engine. Server: http://192.168.0.140:3000 2021-03-02 15:17:46.529361+0700 WebsocketTest[1641:1367063] LOG SocketEngine: Handshaking 2021-03-02 15:17:46.590552+0700 WebsocketTest[1641:1367063] LOG SocketEnginePolling: Doing polling GET http://192.168.0.140:3000/socket.io/?transport=polling&b64=1 2021-03-02 15:17:46.900905+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: Got polling response 2021-03-02 15:17:46.901168+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: Got poll message: 0{"sid":"lIiAqYjNv8DdbC0QAABI","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2021-03-02 15:17:46.902669+0700 WebsocketTest[1641:1367066] LOG SocketEngine: Got message: 0{"sid":"lIiAqYjNv8DdbC0QAABI","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2021-03-02 15:17:46.907785+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: ping with data: [] Event: Socket pinged[] 2021-03-02 15:17:46.907938+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: Doing polling GET http://192.168.0.140:3000/socket.io/?transport=polling&b64=1&sid=lIiAqYjNv8DdbC0QAABI 2021-03-02 15:17:46.907958+0700 WebsocketTest[1641:1367041] LOG SocketManager: Engine opened Connect 2021-03-02 15:17:46.908031+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Socket connected 2021-03-02 15:17:46.908094+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3] 2021-03-02 15:17:46.908197+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: connect with data: ["/"] Socket Connected 2021-03-02 15:17:46.908316+0700 WebsocketTest[1641:1367066] LOG SocketEngine: Writing poll: has data: false 2021-03-02 15:17:46.908375+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: Sending poll: as type: 2 2021-03-02 15:17:46.908642+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: Created POST string: 1:2 2021-03-02 15:17:46.909137+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: POSTing 2021-03-02 15:17:46.909291+0700 WebsocketTest[1641:1367066] LOG SocketEnginePolling: Doing polling POST http://192.168.0.140:3000/socket.io/?transport=polling&b64=1&sid=lIiAqYjNv8DdbC0QAABI 2021-03-02 15:17:46.916622+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["sec-websocket-accept": "KvCWVitTVryRfEyi6dYrWIEDbHo=", "connection": "Upgrade", "upgrade": "websocket"]] 2021-03-02 15:17:46.916695+0700 WebsocketTest[1641:1367067] LOG SocketEngineWebSocket: Sending ws: probe as type: 2 2021-03-02 15:17:46.921154+0700 WebsocketTest[1641:1367067] LOG SocketEngine: Flushing probe wait 2021-03-02 15:17:46.921753+0700 WebsocketTest[1641:1367067] LOG SocketEnginePolling: Got polling response 2021-03-02 15:17:46.921873+0700 WebsocketTest[1641:1367067] LOG SocketEnginePolling: Doing polling GET http://192.168.0.140:3000/socket.io/?transport=polling&b64=1&sid=lIiAqYjNv8DdbC0QAABI 2021-03-02 15:17:46.927394+0700 WebsocketTest[1641:1367063] ERROR SocketEnginePolling: Error during long poll request 2021-03-02 15:17:46.927458+0700 WebsocketTest[1641:1367063] ERROR SocketEngine: Error 2021-03-02 15:17:46.927550+0700 WebsocketTest[1641:1367063] LOG SocketEngine: Engine is being closed. 2021-03-02 15:17:46.927549+0700 WebsocketTest[1641:1367041] ERROR SocketManager: Error 2021-03-02 15:17:46.927625+0700 WebsocketTest[1641:1367063] LOG SocketEnginePolling: Created POST string: 1:1 2021-03-02 15:17:46.927632+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: error with data: ["Error"] Event: Error, ERROR Occured ["Error"] 2021-03-02 15:17:46.927794+0700 WebsocketTest[1641:1367063] LOG SocketEnginePolling: Doing polling POST http://192.168.0.140:3000/socket.io/?transport=polling&b64=1&sid=lIiAqYjNv8DdbC0QAABI 2021-03-02 15:17:46.928106+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Disconnected: Error 2021-03-02 15:17:46.928168+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: statusChange with data: [disconnected, 1] 2021-03-02 15:17:46.928313+0700 WebsocketTest[1641:1367041] LOG SocketIOClient{/}: Handling event: disconnect with data: ["Error"] Socket Disconnect

Any help would be appreciated, thanks in advance.

nuclearace commented 3 years ago

What's the client version / server version? If you're trying to connect to a socket.io 2.0 server with 16 client, you'll need to pass the .version(.two) option.

michaellie99 commented 3 years ago

What's the client version / server version? If you're trying to connect to a socket.io 2.0 server with 16 client, you'll need to pass the .version(.two) option.

Im using both the latest available socketio (3.0) and client(?) I dont know how to check the client version but I'm using socketio v15.2.0 for the pod installation Where do I pass this version option? It doesn't show up in the configs

nuclearace commented 3 years ago

@michaellie99 for socket.io 3, you need to use v16

michaellie99 commented 3 years ago

@nuclearace thanks, Installing v16 works, I was kinda confused because the latest release version was 15.2.0, to the people having the same issue as me, the fix is changing the version of socket.io installed in the Podfile (or carthage, etc if you dont use cocoapods) to '~> 16.0.1'.