作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用具有 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/
我是一名优秀的程序员,十分优秀!