升级后socket.io客户端swift从9.0.1升级到13.2.1无法连接

时间:2018-05-31 01:44:53

标签: swift sockets socket.io loopbackjs

使用与客户端9.0.1版本的AWS中的套接字服务器相同的代码在升级后不能与13.2.1一起使用。  在升级之前,代码是以下

lazy var socket:SocketIOClient = {
    //let cookieStorage = HTTPCookieStorage.shared
    //let cookies = cookieStorage.cookies
    return SocketIOClient(socketURL: URL(string: baseUrl)!, config: [.log(true), .forcePolling(true), .reconnectAttempts(-1), .reconnects(true), .reconnectWait(5), .doubleEncodeUTF8(true)])
}()
func socketInit(){


        socket.on("connect") {data, ack in
            print("socket connected")
            let accessToken = TrueketekeClient.instance.accessToken as! String
            self.socket.emit("authentication", ["id": accessToken, "userId": User.current!.id as! String])

        }

        socket.on("authenticated") {data, ack in
            print("socket authenticated", data)
            self.sendEnabled = true
            if self.room != nil {
                self.joinRoom()
            }

        }

        socket.on("room:join") {data, ack in
            print(data)
            if self.room == nil {
                self.newRoom = true
                let dictData = data[0] as! [String:Any]
                let room:Room? = Mapper<Room>().map(JSON: dictData)
                self.room = room
                self.newRoom = false
                AnswerLog.chat()
            }

            if let title = self.room?.trueke?.title {
                self.title = title
            }

            self.emitMessageQueue()

        }

        socket.on("message:readedAll") {data, ack in
            if let userId = data[0] as? String{
                if userId != User.current?.id as? String {
                    self.dataSource.updateReadedStatus(User.current?.id as? String)
                }
            }

        }

        socket.on("message:send") {data, ack in
            let dictData = data[0] as! [String:Any]
            let message:Message? = Mapper<Message>().map(JSON: dictData)

            if let index = self.messageQueue.index(where: {$0.uid  == message?.uid}){
                self.messageSender.updateMessage(self.messageQueue[index], status: .success, readed: message?.unreaded == nil)
                self.messageQueue.remove(at: index)
            }else{
                if message != nil {
                    self.addMessage(message!)
                }
            }

           /* if message?.userId as? String != User.current?.id as? String {
                self.dataSource.updateReadedStatus(User.current?.id as? String)
            }*/

        }

        socket.on("error") {data, ack in

            self.errorQueue()


        }

        socket.on("disconnect") {data, ack in
            if self.socket.status == .notConnected || self.socket.status == .disconnected {
               // self.socket.connect()
            }

            self.leaveRoom()

            self.errorQueue()


        }

        /*socketClient.on("currentAmount") {data, ack in
            if let cur = data[0] as? Double {
                socketClient.emitWithAck("canUpdate", cur)(timeoutAfter: 0) {data in
                    socketClient.emit("update", ["amount": cur + 2.50])
                }

                ack.with("Got your currentAmount", "dude")
            }
        }*/

        socket.connect()

    }

并在 viewWillApper

中调用此方法
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    socketInit()
}

将库升级到版本13.2.1之后的新声明。

var socket:SocketIOClient = SocketManager(socketURL: URL(string: baseUrl)!, config: [.log(true), .forcePolling(true), .reconnectAttempts(-1), .reconnects(true), .reconnectWait(5)]).defaultSocket

并在 viewWillAppear 中调用与上述相同的方法theres不是受控消息,并从控制台获取此消息。

  

2018-05-30 21:38:22.201430-0400 Trueketeke [1598:688036] LOG SocketManager:经理正在发布iniciando Socket 2018-05-30   21:38:22.249842-0400 Trueketeke [1598:688036] LOG SocketIOClient {/}:   添加事件处理程序:connect 2018-05-30 21:38:22.249948-0400   Trueketeke [1598:688036] LOG SocketIOClient {/}:为...添加处理程序   事件:认证2018-05-30 21:38:22.249991-0400   Trueketeke [1598:688036] LOG SocketIOClient {/}:为...添加处理程序   事件:房间:加入2018-05-30 21:38:22.250024-0400   Trueketeke [1598:688036] LOG SocketIOClient {/}:为...添加处理程序   事件:消息:readedAll 2018-05-30 21:38:22.250052-0400   Trueketeke [1598:688036] LOG SocketIOClient {/}:为...添加处理程序   事件:消息:发送2018-05-30 21:38:22.250085-0400   Trueketeke [1598:688036] LOG SocketIOClient {/}:为...添加处理程序   事件:错误2018-05-30 21:38:22.250114-0400 Trueketeke [1598:688036]   LOG SocketIOClient {/}:为事件添加处理程序:disconnect to to   连接2018-05-30 21:38:22.250201-0400 Trueketeke [1598:688036] LOG   SocketIOClient {/}:尝试连接已连接的套接字

所以我看不到指示打印(&#34; socket connected&#34;)的消息所以任何想法为什么没有连接到socket

0 个答案:

没有答案