简体   繁体   English

在 iOS Z818056DBD7E201243206B9C7CD8848C

[英]Emit not working for web sockets in iOS swift?

I have integrated web sockets in my iOS app.我在我的 iOS 应用程序中集成了 web sockets。 The server is written with Nodejs and express .服务器是用Nodejsexpress编写的。 Apart from my iOS app I also have a reactjs web app.除了我的 iOS 应用程序之外,我还有一个reactjs web 应用程序。 The web app works correctly so there is no issue from server side. web 应用程序正常工作,因此服务器端没有问题。 I am using this library for my iOS app https://github.com/socketio/socket.io-client-swift/我将这个库用于我的 iOS 应用程序https://github.com/socketio/socket.io-client-swift/

Now I am able to receive events from my server like if a new user has joined in the iOS app but the iOS app won't emit any event on button click.现在我可以从我的服务器接收事件,例如如果新用户加入了 iOS 应用程序,但 iOS 应用程序不会在按钮单击时发出任何事件。 On viewdidload I have an emit event of userjoin which works perfectly.在 viewdidload 我有一个 userjoin 的发射事件,它工作得很好。 But when I am sending message on a button click that event does not work.但是当我在按钮上发送消息时,单击该事件不起作用。 Here is a gist of my code这是我的代码的要点

let manager = SocketManager(socketURL: URL(string: "MY_URL")!, config: [.log(true), .compress])
    var socket: SocketIOClient? = nil

override func viewDidLoad() {
        super.viewDidLoad()

        socket = manager.defaultSocket

socket?.connect()

        socket?.on("connect") { _, _ in
            self.socket?.emit("userjoin", self.defaults.string(forKey:"email") ?? "")
        }

        socket?.on("userjoined", callback: { (data, ack) in
            self.userJoinedLbl.text = data[0] as? String ?? ""
        }) ...

The above code works perfectly fine.上面的代码工作得很好。 Below code of my button click won't work我的按钮点击下面的代码不起作用

@IBAction func sendMsgClicked(_ sender: Any) {
        debugPrint(self.defaults.string(forKey:"id") ?? "")
        socket?.on("connect") { _, _ in
            self.socket?.emit("sendMessage", self.sendMsgTextField.text ?? "",self.defaults.string(forKey:"id") ?? "")
        }

    }

I am getting following logs from the web socket library我正在从 web 套接字库中获取以下日志

2020-04-05 15:09:58.032340+0530 WebSocketNativeIOS[5995:216223] LOG SocketIOClient{/}: Adding handler for event: connect
2020-04-05 15:10:17.211199+0530 WebSocketNativeIOS[5995:216223] LOG SocketIOClient{/}: Handling event: ping with data: []
2020-04-05 15:10:17.211211+0530 WebSocketNativeIOS[5995:218688] LOG SocketEngine: Writing ws:  has data: false
2020-04-05 15:10:17.211587+0530 WebSocketNativeIOS[5995:218688] LOG SocketEngineWebSocket: Sending ws:  as type: 2
2020-04-05 15:10:17.418903+0530 WebSocketNativeIOS[5995:218688] LOG SocketEngine: Got message: 3
2020-04-05 15:10:17.419593+0530 WebSocketNativeIOS[5995:216223] LOG SocketIOClient{/}: Handling event: pong with data: []

I am emitting an sendMessage event which takes the user message and user id.我正在发出一个 sendMessage 事件,该事件采用用户消息和用户 ID。 The user message and user id are not null.用户消息和用户 ID 不是 null。 The sendMessage event works fine on the web app sendMessage 事件在 web 应用程序上运行良好

The code for my web app when the button is clicked is单击按钮时我的 web 应用程序的代码是

socket.emit("sendMessage", msg, localStorage.getItem("userId"));

Ok I figured it out好的,我想通了

I added this in my button click我在我的按钮点击中添加了这个

manager.defaultSocket.emit("sendMessage", self.sendMsgTextField.text ?? "",self.defaults.string(forKey:"id") ?? "")

Thanks to this issue https://github.com/socketio/socket.io-client-swift/issues/1055 I was able to figure it out由于这个问题https://github.com/socketio/socket.io-client-swift/issues/1055我能够弄清楚

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

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