4

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
Nastromo
  • 622
  • 1
  • 9
  • 16

2 Answers2

7

So, to solve this issue we have to do a couple of actions:

  1. Add a .connectParams(["token": token]) for manager.config - in my case I have to use "token" key as URL parameter.
  2. Add a .secure(true) parameter for manager.config.
  3. Add an "App Transport Security Settings" key in info.plist (Dictionary type) and sub key "Allow Arbitrary Loads" (boolean type) with YES value.

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()
    }
}
Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
Nastromo
  • 622
  • 1
  • 9
  • 16
6

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.

pacification
  • 5,838
  • 4
  • 29
  • 51
  • 1
    Yes, it's solved my issue. But there are more settings we have to add to run this bad boy. See my answer below if someone faced the same problem. – Nastromo Mar 26 '18 at 15:04