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
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With