gpt4 book ai didi

ios - 如何在iOS Swift中连接套接字?

转载 作者:行者123 更新时间:2023-12-03 12:07:55 31 4
gpt4 key购买 nike

我正在尝试使用具有 token 和 namespace 的api连接套接字。显示错误引擎未打开时尝试连接 socket 。正在连接,长时间轮询请求时出错

这是我的套接字代码:

  import UIKit
import SocketIO

class SocketIOManager: NSObject {

static let authToken = UserDefaults.standard.string(forKey: "authToken")
static let bearerToken: String = "Bearer " + (authToken ?? "")
static let newBaseURL = UserDefaults.standard.string(forKey: "baseURL")!
//static let v = newBaseURL + "/event-stream/?token=\(bearerToken)"

static let v = newBaseURL



static let manager = SocketManager(socketURL: URL(string: v)!, config: [.log(true), .compress, .forcePolling(false), .forceNew(true), .reconnects(true), .reconnectAttempts(100), .forceWebsockets(true)])
static let socket = manager.socket(forNamespace: "/event-stream/")



class func connectSocket(){
self.manager.config = SocketIOClientConfiguration(
arrayLiteral: .connectParams(["token": SocketIOManager.bearerToken]), .secure(true)
)
socket.connect()
}

class func disconnectSocket(){
socket.disconnect()
}
}

这是我的错误日志:
    2020-01-27 15:05:36.607140+0530 A8FlowSampleApp[1517:468209] LOG SocketIOClient{/event- stream/}: Handling event: statusChange with data: [connecting, 2]
2020-01-27 15:05:36.607465+0530 A8FlowSampleApp[1517:468209] LOG SocketIOClient{/event-
stream/}: Joining namespace /event-stream/
2020-01-27 15:05:36.607579+0530 A8FlowSampleApp[1517:468209] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2020-01-27 15:05:36.607653+0530 A8FlowSampleApp[1517:468209] LOG SocketManager: Adding engine
2020-01-27 15:05:36.612981+0530 A8FlowSampleApp[1517:468391] LOG SocketEngine: Starting engine. Server: api url
2020-01-27 15:05:36.613238+0530 A8FlowSampleApp[1517:468391] LOG SocketEngine: Handshaking
2020-01-27 15:05:36.635760+0530 A8FlowSampleApp[1517:468391] LOG SocketEnginePolling: Doing polling GET https://api url/socket.io/?transport=polling&b64=1&token=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhOGZsb3ciLCJleHAiOjE1ODAyNzcyNzAsInVzZXJuYW1lIjoiZGVtbyJ9.SaoWMoYF-bp57fEXw0-EJAW9JDQZ8fyaYjCirL06M1Y
2020-01-27 15:05:36.990748+0530 A8FlowSampleApp[1517:468395] ERROR SocketEnginePolling: Error during long poll request
2020-01-27 15:05:36.990925+0530 A8FlowSampleApp[1517:468395] ERROR SocketEngine: Error
2020-01-27 15:05:36.991075+0530 A8FlowSampleApp[1517:468209] ERROR SocketManager: Error

任何帮助,不胜感激请...

最佳答案

试试下面的代码

class func connectSocket(){
self.manager.config = SocketIOClientConfiguration(
arrayLiteral: .connectParams(["token": SocketIOManager.bearerToken]), .secure(true)
)

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

//To disconnect socket connection
socket.on(clientEvent: .disconnect) { (data, ack) in
print("socket disconnected")
}
}

关于ios - 如何在iOS Swift中连接套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59928762/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com