Socket.io-client-swift: New Version (Swift4) Not work

Created on 17 Jan 2018  路  8Comments  路  Source: socketio/socket.io-client-swift

import SocketIO

let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])
let socket = manager.defaultSocket

socket.on(clientEvent: .connect) {data, ack in
print("socket connected")
}
socket.connect()

I migrate from swift 3 to swift 4.
I switched to the latest version, I created the SocketManager, but I can not connect to the socket.

question

Most helpful comment

Manager needs to be a property or a global. If it's not, then the manager will be released and it won't work. The actual socket object doesn't need to be stored; you can always access it from defaultSocket or the socket(forNamespace:) method.

All 8 comments

Have you checked the logs? Make sure the manager isn't being released.

@nuclearace I edit my code like this:

let manager = SocketManager(socketURL: myURL, config: [SocketIOClientOption.log(true), SocketIOClientOption.reconnects(true), SocketIOClientOption.sessionDelegate(self), SocketIOClientOption.compress, SocketIOClientOption.extraHeaders(["Cookie" : cookie, "Cache-control": "no-cache"])])
socket = manager.defaultSocket
socket.on("connect") { data, ack in
print("Connesso")
}
socket.on("error") { data, ack in
}
socket.connect()

the log does not appear

Are you running this inside of an app or just a main.swift? If you're not running inside of an app you need to start the main runloop.

The previous version ( I used v12.1.3) worked properly, with this SocketManager no longer.

Well, if logs aren't being printed then something is going very wrong...

Is this syntax correct?

var socket:SocketIOClient! = nil
let manager = SocketManager(socketURL: myURL, config: [SocketIOClientOption.log(true), SocketIOClientOption.reconnects(true), SocketIOClientOption.sessionDelegate(self), SocketIOClientOption.compress, SocketIOClientOption.extraHeaders(["Cookie" : cookie, "Cache-control": "no-cache"])])
socket = manager.defaultSocket
socket.on("connect") { data, ack in
print("Connesso")
}
socket.on("error") { data, ack in
}
socket.connect()

Manager needs to be a property or a global. If it's not, then the manager will be released and it won't work. The actual socket object doesn't need to be stored; you can always access it from defaultSocket or the socket(forNamespace:) method.

ok seems to work again. thanks and close

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heisian picture heisian  路  4Comments

janiduw picture janiduw  路  5Comments

roma86 picture roma86  路  4Comments

gaetanm picture gaetanm  路  4Comments

njawahire picture njawahire  路  4Comments