I have read the latest documentation of Socket.io for Swift. And there is an example of a new connection:
let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])
let socket = manager.defaultSocket
So I have created a class SocketIOManager, like this:
class SocketIOManager: NSObject {
static let manager = SocketManager(socketURL: URL(string: "myurl.com:443"))
static let socket = manager.defaultSocket
class func connectSocket(){
socket.connect()
}
class func reciveMessage(){
socket.on("new-message-mob") { (dataArray, ack) in
print(dataArray.count)
}
}
}
And then I just invoke the method SocketIOManager.connectSocket() in my ViewController. But the server produce the error. I don't develop the server side. I just need to know - did I create connection properly? How do you connect to socket through Socket.io these days?
P.S Server error - jwt must be provided (it seems like no token, but there is).
UPDATE (with dummy values):
I'm passing token like this:
static let manager = SocketManager(socketURL: URL(string: "https://myurl:443?token=\(token)")!)
static let socket = manager.defaultSocket
It looks like you set the token
in wrong place. According to the issue, token
should be set as header parameter in configuration:
manager.config = SocketIOClientConfiguration(
arrayLiteral: .compress, .connectParams(["Authorization": token])
)
Try to manage your token
this way.
So, to solve this issue we have to do a couple of actions:
CODE:
class SocketIOManager: NSObject {
static let manager = SocketManager(socketURL: URL(string: "https://yoururl.com:443")!, config: [.log(true), .compress])
static let socket = manager.defaultSocket
class func connectSocket(){
self.manager.config = SocketIOClientConfiguration(
arrayLiteral: .connectParams(["token": token], .secure(true)
)
socket.connect()
}
class func disconnectSocket(){
socket.disconnect()
}
}
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