Hikaru Yoshizaki
Hikaru Yoshizaki

Reputation: 53

Socket.io with Swift is not handshaking

I'm trying to integrate iOS app with Node.js WebSocket Server. I'm using SwiftUI for the iOS and socket.io for both server and client.

But it when I am trying to connect the iOS app with the server, It seems to not connect with the server.

I tested if the server is running by connecting a React app to the Node.js server and the connection is working just fine with the browser.

I've checked the swift logs and It seems trying to handshake with the server but, it starts long polling so I'm guessing the handshaking process has some kinda issue.

I'm console logging "new connection" as a string in Node.js server if there is a new connection and, it's working fine for React but not Swift.

But I'm not trying anything advance so I can't find out what is wrong.

Can someone help me out pls?

import SwiftUI
import SocketIO

final class Service: ObservableObject {
    private var manager = SocketManager(socketURL:URL(string: "http://localhost:5000")!, config: [.log(true), .compress])
    
    init() {
        let socket = manager.defaultSocket
        socket.on(clientEvent: .connect)  { (data,act) in
            print("Connected")
        }
        socket.connect()
    }
}

struct SocketIO: View {
    @ObservedObject var service = Service()
    var body: some View {
        Text("Hello, Web socket!")
    }
}

This is the log I'm getting from SocketIO-client-swift

2020-12-18 13:08:24.033132+0900 Tests[4397:171225] LOG SocketIOClient{/}: Adding handler for event: connect 2020-12-18 13:08:24.033663+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] 2020-12-18 13:08:24.033811+0900 Tests[4397:171225] LOG SocketIOClient{/}: Joining namespace / 2020-12-18 13:08:24.033930+0900 Tests[4397:171225] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting 2020-12-18 13:08:24.034033+0900 Tests[4397:171225] LOG SocketManager: Adding engine 2020-12-18 13:08:24.035507+0900 Tests[4397:171427] LOG SocketEngine: Starting engine. Server: http://localhost:5000 2020-12-18 13:08:24.035608+0900 Tests[4397:171427] LOG SocketEngine: Handshaking 2020-12-18 13:08:24.038113+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1 2020-12-18 13:08:24.125554+0900 Tests[4397:171431] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed 2020-12-18 13:08:24.166652+0900 Tests[4397:171427] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.166801+0900 Tests[4397:171427] LOG SocketEnginePolling: Got poll message: 0{"sid":"97Fb8N3Ub5ZuRlfHAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.168076+0900 Tests[4397:171427] LOG SocketEngine: Got message: 0{"sid":"97Fb8N3Ub5ZuRlfHAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.171813+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: ping with data: [] 2020-12-18 13:08:24.171845+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.172148+0900 Tests[4397:171225] LOG SocketManager: Engine opened Connect 2020-12-18 13:08:24.172267+0900 Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-18 13:08:24.172485+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3] 2020-12-18 13:08:24.172704+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: connect with data: ["/"] Connected 2020-12-18 13:08:24.174125+0900 Tests[4397:171427] LOG SocketEngine: Writing poll: has data: false 2020-12-18 13:08:24.174260+0900 Tests[4397:171427] LOG SocketEnginePolling: Sending poll: as type: 2 2020-12-18 13:08:24.174607+0900 Tests[4397:171427] LOG SocketEnginePolling: Created POST string: 1:2 2020-12-18 13:08:24.175188+0900 Tests[4397:171427] LOG SocketEnginePolling: POSTing 2020-12-18 13:08:24.175708+0900 Tests[4397:171427] LOG SocketEnginePolling: Doing polling POST http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.181566+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["upgrade": "websocket", "connection": "Upgrade", "sec-websocket-accept": "DK4ZA/luKjOL11P+2vm4APQNE5I="]] 2020-12-18 13:08:24.181780+0900 Tests[4397:171429] LOG SocketEngineWebSocket: Sending ws: probe as type: 2 2020-12-18 13:08:24.183557+0900 Tests[4397:171431] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.183723+0900 Tests[4397:171431] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.184529+0900 Tests[4397:171427] LOG SocketEngine: Flushing probe wait 2020-12-18 13:08:24.186620+0900 Tests[4397:171428] ERROR SocketEnginePolling: Error during long poll request 2020-12-18 13:08:24.186736+0900 Tests[4397:171428] ERROR SocketEngine: Error 2020-12-18 13:08:24.186892+0900 Tests[4397:171225] ERROR SocketManager: Error 2020-12-18 13:08:24.186907+0900 Tests[4397:171428] LOG SocketEngine: Engine is being closed. 2020-12-18 13:08:24.187029+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: error with data: ["Error"] 2020-12-18 13:08:24.187306+0900 Tests[4397:171428] LOG SocketEnginePolling: Created POST string: 1:1 2020-12-18 13:08:24.187548+0900 Tests[4397:171428] LOG SocketEnginePolling: Doing polling POST http://localhost:5000/socket.io/?transport=polling&b64=1&sid=97Fb8N3Ub5ZuRlfHAAAA 2020-12-18 13:08:24.188161+0900 Tests[4397:171225] LOG SocketManager: Starting reconnect 2020-12-18 13:08:24.188342+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] 2020-12-18 13:08:24.188558+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: reconnect with data: ["Error"] 2020-12-18 13:08:24.188729+0900 Tests[4397:171225] LOG SocketManager: Trying to reconnect 2020-12-18 13:08:24.188830+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: reconnectAttempt with data: [-1] 2020-12-18 13:08:24.189043+0900 Tests[4397:171429] LOG SocketEngine: Starting engine. Server: http://localhost:5000 2020-12-18 13:08:24.189113+0900 Tests[4397:171225] LOG SocketManager: Scheduling reconnect in 15.068159317825785s 2020-12-18 13:08:24.189439+0900 Tests[4397:171429] LOG SocketEngine: Handshaking 2020-12-18 13:08:24.189732+0900 Tests[4397:171429] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1 2020-12-18 13:08:24.197564+0900 Tests[4397:171432] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.197690+0900 Tests[4397:171432] LOG SocketEnginePolling: Got poll message: 0{"sid":"7RPKclRosgovPKY5AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.197839+0900 Tests[4397:171432] LOG SocketEngine: Got message: 0{"sid":"7RPKclRosgovPKY5AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000} 2020-12-18 13:08:24.198569+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: ping with data: [] 2020-12-18 13:08:24.198587+0900 Tests[4397:171432] LOG SocketEnginePolling: Doing polling GET http://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB 2020-12-18 13:08:24.198671+0900 Tests[4397:171225] LOG SocketManager: Engine opened Connect 2020-12-18 13:08:24.198848+0900 Tests[4397:171225] LOG SocketIOClient{/}: Socket connected 2020-12-18 13:08:24.199022+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: statusChange with data: [connected, 3] 2020-12-18 13:08:24.199197+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: connect with data: ["/"] Connected 2020-12-18 13:08:24.199445+0900 Tests[4397:171432] LOG SocketEngine: Writing poll: has data: false 2020-12-18 13:08:24.199714+0900 Tests[4397:171432] LOG SocketEnginePolling: Sending poll: as type: 2 2020-12-18 13:08:24.199973+0900 Tests[4397:171432] LOG SocketEnginePolling: Created POST string: 1:2 2020-12-18 13:08:24.200257+0900 Tests[4397:171432] LOG SocketEnginePolling: POSTing 2020-12-18 13:08:24.200524+0900 Tests[4397:171432] LOG SocketEnginePolling: Doing polling POST http://localhost:5000/socket.io/?transport=polling&b64=1&sid=7RPKclRosgovPKY5AAAB 2020-12-18 13:08:24.205180+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: websocketUpgrade with data: [["upgrade": "websocket", "connection": "Upgrade", "sec-websocket-accept": "e5m4DoLk6GlrzUzZSHtQ/n0gXl0="]] 2020-12-18 13:08:24.205309+0900 Tests[4397:171429] LOG SocketEngineWebSocket: Sending ws: probe as type: 2 2020-12-18 13:08:24.207171+0900 Tests[4397:171429] LOG SocketEngine: Got message: 3probe 2020-12-18 13:08:24.207420+0900 Tests[4397:171429] LOG SocketEngine: Received probe response, should upgrade to WebSockets 2020-12-18 13:08:24.207622+0900 Tests[4397:171429] LOG SocketEngine: Upgrading transport to WebSockets 2020-12-18 13:08:24.209704+0900 Tests[4397:171429] LOG SocketEnginePolling: Sending poll: as type: 6 2020-12-18 13:08:24.210114+0900 Tests[4397:171225] LOG SocketIOClient{/}: Handling event: pong with data: [] 2020-12-18 13:08:24.210826+0900 Tests[4397:171431] LOG SocketEngine: Flushing probe wait 2020-12-18 13:08:24.211131+0900 Tests[4397:171431] LOG SocketEnginePolling: Got polling response 2020-12-18 13:08:24.211279+0900 Tests[4397:171431] LOG SocketEngine: Switching to WebSockets 2020-12-18 13:08:24.211500+0900 Tests[4397:171431] LOG SocketEngineWebSocket: Sending ws: as type: 5 2020-12-18 13:08:24.211974+0900 Tests[4397:171431] LOG SocketEngine: Flushing probe wait

Upvotes: 4

Views: 3055

Answers (1)

It could be related to security. Typically ios apps expect connections using https. To allow http connections, in your info.plist add this (temporary security fix):

<key>NSAppTransportSecurity</key>
<dict>

 <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

Upvotes: 1

Related Questions