Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io official client connection issues?

I am working with iOS 8 and Swift. I want to use the official socket.io client but for some reason it does not attempt to connect. I followed the example given here: https://github.com/socketio/socket.io-client-swift

let socket = SocketIOClient(socketURL: "\(CurrentConfiguration.serverURL)") 

socket.reconnects = true
socket.reconnectWait = 10
socket.nsp = "/messagelist"

// Connect
socket.connect()

socket.onAny {println("got event: \($0.event) with items \($0.items)")} 
socket.on("connect") {data, ack in
    println("socket connected")
}

socket.on("error") {data in
    println("socket ERROR")
    println(data)
}

Can anyone confirm this? Is this a version problem or maybe related to Swift 1.2?

On the server side i cant even recognize a connection attempt. The variable serverURL is the same as i had before and

like image 703
longbow Avatar asked Apr 15 '15 17:04

longbow


People also ask

How do I fix a Socket connection error?

This error occurs when the socket connection to the remote server is denied. The possible reasons are a firewall or antivirus (AV) software is blocking the connection, the port used is blocked or unreachable. Temporarily disable firewall/AV program and check the connection after restarting your computer.

Why is Socket not connected?

The error message “ERR_SOCKET_NOT_CONNECTED” is instantly solved in the majority of the cases when we flush the sockets on your browser. This will break the connection between any active pages on your browser and you might have to reinitialize everything.


1 Answers

Make sure to call socket.connect() after you call your handlers. A good practice would be to create a function with all of your required handlers, and call it in the viewDidLoad method. Aftewards you would then call the connect method.

Example:

let socket = SocketIOClient(socketURL: "URL") 

override func viewDidLoad() {
   super.viewDidLoad()

   // socket.io
   addHandlers()
   socket.connect()
}

func addHandlers() {

   self.socket.onAny {println("Got event: \($0.event), with items: \($0.items)")}

   self.socket.on("processReq") {[weak self] data, ack in
   // handler code here
   }

}
like image 77
rambossa Avatar answered Nov 15 '22 22:11

rambossa