HomeiOS DevelopmentMaking a number of connections in socket io with Swift

Making a number of connections in socket io with Swift


I am constructing a messaging app with Swift. There are rooms on the house display screen of my software, and when I’m on this display screen, I can take heed to the message from which room with socket io, and this example by no means closes whereas within the software. I’m continually listening to the rooms to indicate which room the message is coming from. After I click on on one of many rooms, I can shoot the messages of the room I clicked on and press the display screen, and I can take heed to the messages coming to that room with socket io once more. After I depart the room, I cease listening to that socket. However I’m having an issue. At first, I haven’t got an issue listening to the messages on the Dwelling display screen, however after coming into and leaving any room, I can’t take heed to the messages approaching the house display screen. Incoming messages seem within the log, however once I debug, they by no means enter socket.on.

extension HomeViewController {
    func setupSocketIO() {
        let userId = userTokenService.getLoggedUser()!.userId
        supervisor.setConfigs([.connectParams(["room": userId])])
        socket = supervisor.defaultSocket
        socket.join()
    }

    func addHandler() {
        socket.on("get_message") { information, _ in
            if let message = information[0] as? [String: Any] {
                let homeGetMessage = HomeGetMessage(content material: message["content"] as! String, phoneNo: message["phoneNo"] as! String, roomId: message["roomId"] as! Int, sentAt: message["sentAt"] as! String)

                self.newMessage(homeGetMessage: homeGetMessage)
            }
        }
        socket.on("create_room") { _, _ in
            self.getAllRoom()
        }
    }
}

extension MessageViewController {
    func addHandler() {
        socket.on("get_message") { information, _ in
            if let message = information[0] as? [String: Any],
               let content material = message["content"] as? String,
               let userId = message["userId"] as? String,
               let sentAt = message["sentAt"] as? String
            {
                let getMessage = GetMessage(content material: content material, userId: userId, sentAt: sentAt)
                DispatchQueue.predominant.async {
                    self.newMessage(userId: getMessage.userId, textual content: getMessage.content material, sentByMe: false)
                }
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments