简体   繁体   English

Swift Socket.io未连接后立即发送的事件

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

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. 连接后似乎无法正常工作。

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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM