Skip to content

Socket emit is not working #458

@suhair

Description

@suhair

I use the emit method as follows. If i remove the socket.connect() from the sendTextMessage method then emit is not triggered on the server side. If connect is called inside the method it works for sometime and then stops. sendTextMessage is triggered by button tap. Any help is appreciated.

class SocketIOManager: NSObject {
    static let sharedImstance = SocketIOManager()
    var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "http://localhost:3030")!, options: [.Log(false), .ForcePolling(true)])


    override init() {
        super.init()
    }


    func establishConnection(){
        socket.connect()


        socket.on("hello") { (data, ack) in
            print("Hello is triggered", data);
        }

        sendTextMessage()
    }

    func closeConnection(){
        socket.disconnect()
    }


    func sendTextMessage(){
        socket.connect()
        socket.emit("connectUser", "Dummy data")

    }


}

In the above code connect is triggered twice and hence two sockets are connected with the server and it seems wrong to me. Please help me. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions