socketio / socket.io-client-swift

Other
5.21k stars 839 forks source link

Socket io gives a connection error when connecting back from going to background #1455

Open Valeriegard opened 12 months ago

Valeriegard commented 12 months ago

It used to be fine with earlier versions of rep, but now on reconnection, it gives this error. Then it reconnects successfully after around 20s. Don't really know where to look, because I didn't change any connection parameters before updating the socket version. Could someone give a direction on how to fix this?

DID ENTER BACKGROUND 2023-08-31 12:36:58.188967+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Closing socket 2023-08-31 12:36:58.189476+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Disconnected: Namespace leave 2023-08-31 12:36:58.189511+0200 iStageViewer[3155:1073334] LOG SocketEngine: Writing poll: 1/, has data: false 2023-08-31 12:36:58.189801+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Handling event: statusChange with data: [disconnected, 1] 2023-08-31 12:36:58.189805+0200 iStageViewer[3155:1073334] LOG SocketEnginePolling: Sending poll: 1/, as type: 4 2023-08-31 12:36:58.190146+0200 iStageViewer[3155:1073334] LOG SocketEnginePolling: Created POST string: 41/, STATUS disconnected 2023-08-31 12:36:58.190831+0200 iStageViewer[3155:1073334] LOG SocketEnginePolling: POSTing 2023-08-31 12:36:58.191099+0200 iStageViewer[3155:1073334] LOG SocketEnginePolling: Doing polling POST http://192.168.2.48:3500/socket.io/?transport=polling&b64=1&EIO=4&sid=wKeQeDFiCyeUyFMfAAA4 2023-08-31 12:36:58.191918+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Handling event: disconnect with data: ["Namespace leave"] disconnect ["Namespace leave"] "socket disconnected" RECONNECTIG ON ENTERING FOREGROUND 2023-08-31 12:37:01.864255+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting, 2] STATUS connecting 2023-08-31 12:37:01.865579+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Joining namespace / 2023-08-31 12:37:01.866159+0200 iStageViewer[3155:1073336] LOG SocketEngine: Writing poll: 0/,{"stationNameFromClient":"val old phone","password”:”1”,”applicationType”:”3”,”stationSerial":"9D0945F3-10BA-4C4F-AD02-6CD0397B03B9"} has data: false 2023-08-31 12:37:01.866445+0200 iStageViewer[3155:1073336] LOG SocketEnginePolling: Sending poll: 0/,{"stationNameFromClient":"val old phone","password”:”1”,”applicationType”:”3”,”stationSerial":"9D0945F3-10BA-4C4F-AD02-6CD0397B03B9"} as type: 4 2023-08-31 12:37:01.866708+0200 iStageViewer[3155:1073336] LOG SocketEnginePolling: Created POST string: 40/,{"stationNameFromClient":"val old phone","password”:”1”,”applicationType”:”3”,”stationSerial":"9D0945F3-10BA-4C4F-AD02-6CD0397B03B9"} 2023-08-31 12:37:01.867208+0200 iStageViewer[3155:1073336] LOG SocketEnginePolling: POSTing 2023-08-31 12:37:01.867474+0200 iStageViewer[3155:1073336] LOG SocketEnginePolling: Doing polling POST http://192.168.2.48:3500/socket.io/?transport=polling&b64=1&EIO=4&sid=wKeQeDFiCyeUyFMfAAA4 2023-08-31 12:37:01.877771+0200 iStageViewer[3155:1073334] [connection] nw_read_request_report [C1] Receive failed with error "Software caused connection abort" 2023-08-31 12:37:01.878772+0200 iStageViewer[3155:1073334] [connection] nw_read_request_report [C2] Receive failed with error "Software caused connection abort" 2023-08-31 12:37:01.879314+0200 iStageViewer[3155:1073334] [connection] nw_flow_add_read_request [C3 192.168.2.48:3500 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] already delivered final read, cannot accept read requests 2023-08-31 12:37:01.879649+0200 iStageViewer[3155:1073334] [connection] nw_read_request_report [C3] Receive failed with error "No message available on STREAM" 2023-08-31 12:37:01.883530+0200 iStageViewer[3155:1073334] Task <37171CA5-AA64-4A4B-8E7D-60493FA85A39>.<10> HTTP load failed, 324/0 bytes (error code: -1005 [4:-4]) 2023-08-31 12:37:01.894391+0200 iStageViewer[3155:1073332] Task <37171CA5-AA64-4A4B-8E7D-60493FA85A39>.<10> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSUnderlyingError=0x1636a7450 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x163f55de0 [0x1f8e51b20]>{length = 16, capacity = 16, bytes = 0x10020dacc0a802300000000000000000}, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <37171CA5-AA64-4A4B-8E7D-60493FA85A39>.<10>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <37171CA5-AA64-4A4B-8E7D-60493FA85A39>.<10>" ), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=http://192.168.2.48:3500/socket.io/?transport=polling&b64=1&EIO=4&sid=wKeQeDFiCyeUyFMfAAA4, NSErrorFailingURLKey=http://192.168.2.48:3500/socket.io/?transport=polling&b64=1&EIO=4&sid=wKeQeDFiCyeUyFMfAAA4, _kCFStreamErrorDomainKey=4} 2023-08-31 12:37:01.895758+0200 iStageViewer[3155:1073332] ERROR SocketEnginePolling: The network connection was lost. 2023-08-31 12:37:01.895907+0200 iStageViewer[3155:1073332] ERROR SocketEngine: The network connection was lost. 2023-08-31 12:37:01.896054+0200 iStageViewer[3155:1073114] ERROR SocketManager: The network connection was lost. 2023-08-31 12:37:01.896189+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Handling event: error with data: ["The network connection was lost."] ERROR The network connection was lost. error ["The network connection was lost."] 2023-08-31 12:37:01.896326+0200 iStageViewer[3155:1073332] LOG SocketEngine: Engine is being closed. "socket error" 2023-08-31 12:37:01.896565+0200 iStageViewer[3155:1073332] LOG SocketEnginePolling: Created POST string: 1 2023-08-31 12:37:01.896855+0200 iStageViewer[3155:1073332] LOG SocketEnginePolling: Doing polling POST http://192.168.2.48:3500/socket.io/?transport=polling&b64=1&EIO=4&sid=wKeQeDFiCyeUyFMfAAA4 2023-08-31 12:37:01.897413+0200 iStageViewer[3155:1073114] LOG SocketManager: Starting reconnect 2023-08-31 12:37:01.897513+0200 iStageViewer[3155:1073114] LOG SocketManager: Trying to reconnect 2023-08-31 12:37:01.897630+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Handling event: reconnectAttempt with data: [-1] 2023-08-31 12:37:01.897818+0200 iStageViewer[3155:1073333] LOG SocketEngine: Starting engine. Server: http://192.168.2.48:3500 2023-08-31 12:37:01.897899+0200 iStageViewer[3155:1073114] LOG SocketManager: Scheduling reconnect in 20.854792953615465s 2023-08-31 12:37:01.897905+0200 iStageViewer[3155:1073333] LOG SocketEngine: Handshaking 2023-08-31 12:37:01.898301+0200 iStageViewer[3155:1073333] LOG SocketEnginePolling: Doing polling GET http://192.168.2.48:3500/socket.io/?transport=polling&b64=1&EIO=4 2023-08-31 12:37:01.900019+0200 iStageViewer[3155:1073334] [connection] nw_flow_add_write_request [C2 192.168.2.48:3500 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] cannot accept write requests 2023-08-31 12:37:01.900128+0200 iStageViewer[3155:1073334] [connection] nw_write_request_report [C2] Send failed with error "Socket is not connected" 2023-08-31 12:37:01.900594+0200 iStageViewer[3155:1073334] [connection] nw_flow_add_write_request [C2 192.168.2.48:3500 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] cannot accept write requests 2023-08-31 12:37:01.900698+0200 iStageViewer[3155:1073334] [connection] nw_write_request_report [C2] Send failed with error "Socket is not connected" 2023-08-31 12:37:01.952923+0200 iStageViewer[3155:1073334] ERROR SocketEnginePolling: Error during long poll request 2023-08-31 12:37:01.953280+0200 iStageViewer[3155:1073334] ERROR SocketEngine: Error 2023-08-31 12:37:01.953640+0200 iStageViewer[3155:1073114] ERROR SocketManager: Error 2023-08-31 12:37:01.953982+0200 iStageViewer[3155:1073114] LOG SocketIOClient{/}: Handling event: error with data: ["Error"] ERROR Error error ["Error"] "socket error"

Here's the code:

SceneDelegate:

` func sceneWillEnterForeground(_ scene: UIScene) {

    if !SharedValues.shared.isStartVC {
        print("RECONNECTIG ON ENTERING FOREGROUND")

        socketManager.establishConnection(stationName: UserDefaults.standard.string(forKey: "stationName")!, timeout: 0, password: UserDefaults.standard.string(forKey: "password")!) { //[weak self] in
        }
    }

}

func sceneDidEnterBackground(_ scene: UIScene) {

    if !SharedValues.shared.isStartVC {

    print("DID ENTER BACKGROUND")
        socketManager.closeConnection()
    }
}

`

Socket Manager

` func initializeSocketIO(ip:String, port: String, name: String) {

    manager = SocketManager(socketURL: URL(string: "http://\(ip):\(port)")!, config: [.log(true)])

    socket = manager?.socket(forNamespace: "/")     

}

func establishConnection(stationName: String, timeout: Double, password: String, completionHandler: @escaping () -> Void) { socketData?.stationName = stationName

    if !password.isEmpty {
        socketData?.password = password
    }

    socket?.connect(withPayload: socketData?.items, timeoutAfter: timeout) {
                    completionHandler()

        print("Could not connect")
        print(password)
    }

}`