socketio / socket.io-client-swift

Other
5.22k stars 843 forks source link

Unable to connect to the server #1180

Closed AcrylicMadness closed 5 years ago

AcrylicMadness commented 5 years ago

Unable to connect to Socket.io server. The similar code on Android works absolutely fine. My code is:

import UIKit
import SocketIO

class ViewController: UIViewController {

    let manager = SocketManager(socketURL: URL(string: "https://mysocketioserver.com/socket.io/")!, config: [.log(false)])
    let authData = ["Token": "MyToken"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let socket = manager.defaultSocket

        socket.on(clientEvent: .connect) { (data, ack) in
            socket.emit("authorization_req", self.authData, completion: {
                print("Socket Connected and Emited Data")
            })
        }

        socket.onAny { (event) in
            print("The event is: \(event.event)")
        }

        socket.on("authorization_res") { (data, ack) in
            print("Authorization Response")
        }

        socket.connect()

        // CFRunLoopRun() doesn't seem to make any difference
        CFRunLoopRun()
    }
}

In theory after I send socket.emit for "authorization_req", the "authorization_res" event should happen. However backend guys say they don't even see my app connected to the server. The console output looks something like this:

The event is: statusChange
The event is: ping
The event is: statusChange
The event is: connect
The event is: pong
Socket Connected and Emited Data
The event is: websocketUpgrade
The event is: pong
The event is: ping
The event is: pong
The event is: ping
The event is: pong
The event is: ping
The event is: pong
nuclearace commented 5 years ago

First thing: If this is an iOS app, you def don't need to start a run loop.

Are you saying that it logs that? Or is that what you would expect to see? Can you turn on logging and post some initial connection stuff?

AcrylicMadness commented 5 years ago

Removed CFRunLoopRun() and enabled logging. Here's what I get

2019-03-28 15:24:27.102772+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Adding handler for event: connect
2019-03-28 15:24:27.103085+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Adding handler for event: authorization_res
2019-03-28 15:24:27.104119+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2]
The event is: statusChange
2019-03-28 15:24:27.104285+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Joining namespace /
2019-03-28 15:24:27.104417+0300 socket-test[1573:295803] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2019-03-28 15:24:27.104453+0300 socket-test[1573:295803] LOG SocketManager: Adding engine
2019-03-28 15:24:27.107943+0300 socket-test[1573:295862] LOG SocketEngine: Starting engine. Server: https://websocket.domuland.ru/socket.io/
2019-03-28 15:24:27.108023+0300 socket-test[1573:295862] LOG SocketEngine: Handshaking
2019-03-28 15:24:27.109756+0300 socket-test[1573:295862] LOG SocketEnginePolling: Doing polling GET https://websocket.domuland.ru/socket.io/?transport=polling&b64=1
2019-03-28 15:24:27.350137+0300 socket-test[1573:295862] LOG SocketEnginePolling: Got polling response
2019-03-28 15:24:27.350233+0300 socket-test[1573:295862] LOG SocketEnginePolling: Got poll message: 96:0{"sid":"A1mjgovLGPnWxvBKAAAV","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:40
2019-03-28 15:24:27.351008+0300 socket-test[1573:295862] LOG SocketEngine: Got message: 0{"sid":"A1mjgovLGPnWxvBKAAAV","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
2019-03-28 15:24:27.357321+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: ping with data: []
2019-03-28 15:24:27.357361+0300 socket-test[1573:295862] LOG SocketEngine: Got message: 40
The event is: ping
2019-03-28 15:24:27.357460+0300 socket-test[1573:295803] LOG SocketManager: Engine opened Connect
2019-03-28 15:24:27.357521+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Socket connected
2019-03-28 15:24:27.357557+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3]
The event is: statusChange
2019-03-28 15:24:27.357621+0300 socket-test[1573:295862] LOG SocketEnginePolling: Doing polling GET https://websocket.domuland.ru/socket.io/?transport=polling&b64=1&sid=A1mjgovLGPnWxvBKAAAV
2019-03-28 15:24:27.357644+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: connect with data: ["/"]
The event is: connect
2019-03-28 15:24:27.358182+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Emitting: 2["authorization_req",{"Token":"MyToken"}], Ack: false
2019-03-28 15:24:27.358435+0300 socket-test[1573:295803] LOG SocketParser: Parsing 0
2019-03-28 15:24:27.358489+0300 socket-test[1573:295803] LOG SocketParser: Decoded packet as: SocketPacket {type: 0; data: []; id: -1; placeholders: 0; nsp: /}
2019-03-28 15:24:27.358630+0300 socket-test[1573:295862] LOG SocketEngine: Writing poll:  has data: false
2019-03-28 15:24:27.358658+0300 socket-test[1573:295862] LOG SocketEnginePolling: Sending poll:  as type: 2
2019-03-28 15:24:27.358787+0300 socket-test[1573:295862] LOG SocketEnginePolling: Created POST string: 1:2
2019-03-28 15:24:27.359150+0300 socket-test[1573:295862] LOG SocketEnginePolling: POSTing
2019-03-28 15:24:27.359521+0300 socket-test[1573:295862] LOG SocketEnginePolling: Doing polling POST https://websocket.domuland.ru/socket.io/?transport=polling&b64=1&sid=A1mjgovLGPnWxvBKAAAV
2019-03-28 15:24:27.359764+0300 socket-test[1573:295862] LOG SocketEngine: Writing poll: 2["authorization_req",{"Token":"MyToken"}] has data: false
2019-03-28 15:24:27.360405+0300 socket-test[1573:295862] LOG SocketEnginePolling: Sending poll: 2["authorization_req",{"Token":"MyToken"}] as type: 4
2019-03-28 15:24:27.377064+0300 socket-test[1573:295860] LOG SocketEnginePolling: Created POST string: 43:42["authorization_req",{"Token":"MyToken"}]
Socket Connected and Emited Data
2019-03-28 15:24:27.377375+0300 socket-test[1573:295860] LOG SocketEnginePolling: POSTing
2019-03-28 15:24:27.377419+0300 socket-test[1573:295860] LOG SocketEnginePolling: Doing polling POST https://websocket.domuland.ru/socket.io/?transport=polling&b64=1&sid=A1mjgovLGPnWxvBKAAAV
2019-03-28 15:24:27.387456+0300 socket-test[1573:295859] LOG SocketEnginePolling: Got polling response
2019-03-28 15:24:27.387522+0300 socket-test[1573:295859] LOG SocketEnginePolling: Got poll message: 1:3
2019-03-28 15:24:27.387584+0300 socket-test[1573:295859] LOG SocketEngine: Got message: 3
2019-03-28 15:24:27.387657+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: pong with data: []
The event is: pong
2019-03-28 15:24:27.388022+0300 socket-test[1573:295859] LOG SocketEnginePolling: Doing polling GET https://websocket.domuland.ru/socket.io/?transport=polling&b64=1&sid=A1mjgovLGPnWxvBKAAAV
2019-03-28 15:24:27.461701+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["upgrade": "websocket", "sec-websocket-accept": "++R0iPHMrEh0r4w+ji6oJijuGIw=", "date": "Thu, 28 Mar 2019 12", "server": "cloudflare", "expect-ct": "max-age=604800, report-uri=\"https", "cf-ray": "4be994c37b53901d-DME", "connection": "upgrade"]]
The event is: websocketUpgrade
2019-03-28 15:24:27.461760+0300 socket-test[1573:295862] LOG SocketEngineWebSocket: Sending ws: probe as type: 2
2019-03-28 15:24:27.468088+0300 socket-test[1573:295862] LOG SocketEngine: Got message: 3probe
2019-03-28 15:24:27.468156+0300 socket-test[1573:295862] LOG SocketEngine: Received probe response, should upgrade to WebSockets
2019-03-28 15:24:27.468186+0300 socket-test[1573:295862] LOG SocketEngine: Upgrading transport to WebSockets
2019-03-28 15:24:27.468222+0300 socket-test[1573:295862] LOG SocketEnginePolling: Sending poll:  as type: 6
2019-03-28 15:24:27.468289+0300 socket-test[1573:295862] LOG SocketEnginePolling: Created POST string: 1:6
2019-03-28 15:24:27.468498+0300 socket-test[1573:295862] LOG SocketEnginePolling: POSTing
2019-03-28 15:24:27.468575+0300 socket-test[1573:295803] LOG SocketIOClient{/}: Handling event: pong with data: []
The event is: pong
2019-03-28 15:24:27.572843+0300 socket-test[1573:295862] LOG SocketEnginePolling: Got polling response
2019-03-28 15:24:27.572919+0300 socket-test[1573:295862] LOG SocketEnginePolling: Got poll message: 1:6
2019-03-28 15:24:27.572983+0300 socket-test[1573:295862] LOG SocketEngine: Got message: 6
2019-03-28 15:24:27.573062+0300 socket-test[1573:295862] LOG SocketEngine: Switching to WebSockets
2019-03-28 15:24:27.573109+0300 socket-test[1573:295862] LOG SocketEngineWebSocket: Sending ws:  as type: 5
2019-03-28 15:24:27.573187+0300 socket-test[1573:295862] LOG SocketEngine: Flushing probe wait

The handler for 'authorization_res' should run right after I emit a token for 'authorization_req', however it does not happen

nuclearace commented 5 years ago

Hm, it looks like the authorization_req is being sent, but nothing comes back. I'd check with the server people and see if they can put the server into debug mode so that it logs all the stuff socket.io does in the background. The client is connecting to the server, and is interacting with it, so something else must be going on.

AcrylicMadness commented 5 years ago

I was able to finally identify the issue, it was solely my mistake.

I was provided with JavaScript code for the Web app and Java code for the Android app. Both of them used https://websocket.domuland.ru/socket.io/ as Socket.io URL and it worked fine. However on iOS it turned out that I had to use https://websocket.domuland.ru and /socket.io/ as a namespace. So as soon as I fixed the URL and switched to manager.socket(forNamespace: "/socket.io/") everything worked.

Thank you and sorry for bothering and wasting your time.

nuclearace commented 5 years ago

@aKirill1942 No problem.