Ad

How To Connect With Socket.io? Swift 4

- 1 answer

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
Ad

Answer

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.

Ad
source: stackoverflow.com
Ad