如何与Socket.io连接?斯威夫特4 [英] How to connect with Socket.io? Swift 4

查看:110
本文介绍了如何与Socket.io连接?斯威夫特4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 Socket.io的最新文档 (适用于Swift).还有一个新连接的示例:

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

所以我创建了一个类 SocketIOManager ,如下所示:

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)
            }
    }
}

然后我只需在ViewController中调用方法 SocketIOManager.connectSocket().但是服务器产生错误.我不开发服务器端.我只需要知道-我是否正确建立了连接?这些天您如何通过Socket.io连接到套接字?

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服务器错误-必须提供jwt(似乎没有令牌,但有令牌).

P.S Server error - jwt must be provided (it seems like no token, but there is).

更新(带有伪值):

我正在像这样传递令牌:

I'm passing token like this:

static let manager = SocketManager(socketURL: URL(string: "https://myurl:443?token=\(token)")!)
static let socket = manager.defaultSocket

推荐答案

似乎您将token设置在错误的位置.根据问题,应将token设置为标题配置中的参数:

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])
)

尝试以这种方式管理您的token.

Try to manage your token this way.

这篇关于如何与Socket.io连接?斯威夫特4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆