Swift Socket.io the event sent right after connection is not executed

时间秒杀一切 提交于 2019-12-25 09:20:18

问题


So I am trying to send (emit) an event right after the connection.

I am building a chat app, and the only issue I have is that when the user disconnected and connect to the server again, other clients are not informed the reconnected client is back (I think it is because my userlist data at the server end is not updated when the reconnected client is back) So I'm trying to send an event right after the connection to update the userlist data at the server end. But seems like the event is not executed for some reason. Any help would be great!

func establishConnection() {
    socket.connect()

    if UsersViewController.nickname != nil {
        socket.emit("connectWithNewId", UsersViewController.nickname)
    }
}

I have check already that the event can be send from the other place of the code. It just seems not working right after the connection.


回答1:


You should emit the event when the connection is established, try something like this:

func establishConnection() {
    socket.on("connect") { data, ack in
        if UsersViewController.nickname != nil {
            socket.emit("connectWithNewId", UsersViewController.nickname)
        }
    }

    socket.connect()
}


来源:https://stackoverflow.com/questions/41286357/swift-socket-io-the-event-sent-right-after-connection-is-not-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!