Ad

How To Use Namespace In SocketIOClient In Swift

- 1 answer

I am using latest version of SocketIOClient (13.1.1). How to emit or listen using namespace.

Ad

Answer

This should be enough :

class SocketIOManager: NSObject {

    static let sharedInstance = SocketIOManager()

    let manager = SocketManager(socketURL: URL(string: "")!, config: [.log(false), .compress, .forcePolling(false)])

    var avaialableCallBack:(([Any]) -> Void)?

    override init(){
        super.init()
    }



    func establishConnection() {

        let socket = manager.socket(forNamespace: "/consumer")

        socket.on("connect") { (data, ack) -> Void in
            print("socket connected",data,ack)
        }

        socket.on(clientEvent: .disconnect){data, ack in
            print("socket disconnected")
        }

        socket.on("session-available") { (dataArr, ack) -> Void in
            ack.with(true)
            if let sessionAvailableCB = self.avaialableCallBack {
                sessionAvailableCB(dataArr)
            }
        }

        socket.connect()
    }

    func closeConnection() {
        let socket = manager.socket(forNamespace: "/consumer")
        socket.disconnect()
    }

    func emitMessage(message:String,data:[String:Any]){
        let socket = manager.socket(forNamespace: "/consumer")
        socket.emit(message,data)
    }

    func emitMessageWithAck(message:String,data:[String:Any]) -> OnAckCallback{
        let socket = manager.socket(forNamespace: "/consumer")
        return socket.emitWithAck(message, data)
    }



}
Ad
source: stackoverflow.com
Ad