未使用Socket.IO在iOS客户端和服务器之间建立连接

时间:2019-11-04 15:34:33

标签: ios sockets socket.io swift4

我想使用Socket.IO在iOS应用和服务器之间建立连接,我创建了 SocketIOManager 类和建立连接(昵称:字符串),但是代码当我在AppDelegate类内调用该方法时,该方法内部不会运行。

我使用Xcode 10,迅速4

我收到以下日志:

LOG SocketIOClient{/chat}: Adding handler for event: connect
2019-11-04 15:48:31.336121+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Adding handler for event: Disconnect
2019-11-04 15:48:31.336204+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Adding handler for event: statusChange
2019-11-04 15:48:31.336265+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Adding handler for event: disconnect
2019-11-04 15:48:31.336318+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Adding handler for event: errror
2019-11-04 15:48:31.350562+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Handling event: statusChange with data: [connecting, 2]
769+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Joining namespace /chat
2019-11-04 15:48:31.350982+0100 DriverReadyToEat Dev[566:60436] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2019-11-04 15:48:31.351047+0100 DriverReadyToEat Dev[566:60436] LOG SocketManager: Adding engine
2019-11-04 15:48:31.364925+0100 DriverReadyToEat Dev[566:60687] LOG SocketEngine: Starting engine. Server: https://food-dev.yassir.io/mobile
2019-11-04 15:48:31.365025+0100 DriverReadyToEat Dev[566:60687] LOG SocketEngine: Handshaking
2019-11-04 15:48:31.367114+0100 DriverReadyToEat Dev[566:60687] LOG SocketEnginePolling: Doing polling GET https://food-dev.yassir.io/socket.io/?transport=polling&b64=1
2019-11-04 15:48:33.383125+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Got polling response
2019-11-04 15:48:33.383384+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Got poll message: 97:0{"sid":"Sx62CyHi2I2wx1X6AAAP","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
2019-11-04 15:48:33.384747+0100 DriverReadyToEat Dev[566:60655] LOG SocketEngine: Got message: 0{"sid":"Sx62CyHi2I2wx1X6AAAP","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
2019-11-04 15:48:33.403269+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Doing polling GET https://food-dev.yassir.io/socket.io/?transport=polling&b64=1&sid=Sx62CyHi2I2wx1X6AAAP
2019-11-04 15:48:33.403882+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Sending poll:  as type: 2
2019-11-04 15:48:33.404188+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Created POST string: 1:2
2019-11-04 15:48:33.404873+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: POSTing
2019-11-04 15:48:33.405133+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Doing polling POST https://food-dev.yassir.io/socket.io/?transport=polling&b64=1&sid=Sx62CyHi2I2wx1X6AAAP
2019-11-04 15:48:33.416764+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Handling event: ping with data: []
2019-11-04 15:48:33.416945+0100 DriverReadyToEat Dev[566:60436] LOG SocketManager: Engine opened Connect
365+0100 DriverReadyToEat Dev[566:60655] LOG SocketEngine: Writing poll: 0/chat, has data: false
2019-11-04 15:48:33.417465+0100 DriverReadyToEat Dev[566:60655] LOG SocketEnginePolling: Sending poll: 0/chat, as type: 4
2019-11-04 15:48:33.755699+0100 DriverReadyToEat Dev[566:60436] LOG SocketIOClient{/chat}: Handling event: error with data: ["Tried emitting when not connected"]
2019-11-04 15:48:33.780552+0100 DriverReadyToEat Dev[566:60659] LOG SocketEnginePolling: Got polling response
: "drivers", "user_id": "5db700cf3098260001a5", "mode": "socket"]

SocketIOManager

    let manager = SocketManager(socketURL: URL(string: socketCreateRoomUrl)!, config: [.log(true), .compress, .reconnects(true) ])

    var socket:SocketIOClient!

    override init()
    {
        super.init()
        socket = manager.defaultSocket
        socket = manager.socket(forNamespace: "/chat")
    }

    func establishConnection(Nickname:NSString)
    {
        if(socket != nil)
        {
        if(socket.status == .disconnected || socket.status == .notConnected)
        {
            RemoveAllListener()

            socket.on(SocketIOManager.constant_class.Connect as String) {data, ack in
                print("..Check Socket hat_onConnection.....\(data).........")
                 self.iSSocketDisconnected = false;
                self.CreateRoom(Nickname: Nickname);
            }
            socket.on(SocketIOManager.constant_class.network_disconnect as String) {data, ack in
                print("..Check Socket dis Connection.....\(data).........")
                self.iSSocketDisconnected=true;
            }
            socket.on("statusChange") {data, ack in
                print("..statusChange.....\(data).........")

            }
            socket.on("disconnect") {data, ack in
                print("..Check Socket dis Connection.....\(data).........")

            }
            socket.on("errror") {data, ack in
                print("..Check ERROR.....\(data).........")


            }
            socket.connect()

        } 
    }

AppDelegate.swift

@objc func IntitialiseSocket() {
        if(SocketIOManager.sharedInstance.socket.status == .disconnected || SocketIOManager.sharedInstance.socket.status == .notConnected) {

                let driver_id : String =  driverInfo.value(forKey: "driver_id") as! String
                SocketIOManager.sharedInstance.establishConnection(Nickname: driver_id)
            }
        } else {
            NotificationModeWebHit(Mode: "socket")
        }
    }

1 个答案:

答案 0 :(得分:0)

在您的

内部
 override init()
{
    super.init()
    socket = manager.defaultSocket
    socket = manager.socket(forNamespace: "/chat")
    socket.connect()
}