gpt4 book ai didi

ios - Socket.io swift 客户端自动多次重新连接

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:04 24 4
gpt4 key购买 nike

我正在为我的应用程序使用 Socket.IO-Client-Swift', '~> 13.1.0'。我已成功将我的应用程序与 Socket 服务器连接,但问题是,它会多次重新连接。结果,也许我无法向服务器发送任何内容。谁能帮我这个?

这是我的代码:

类 SocketIOManager: NSObject {

var locationReceiverDelegate: LocationReceiverDelegate?
static let shared = SocketIOManager()
let manager = SocketManager(socketURL: URL(string: "server-url")!, config: [.log(true), .connectParams(["token" : "token-string"])])

var socket : SocketIOClient!


override init() {
super.init()

socket = manager.defaultSocket
self.connectSocket()
}

func connectSocket() {

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

self.socket.on("connected") { ( dataArray, ack) in
print("dataArray",dataArray)
}

self.socket.on("error") { (data, ack) in
print("error-->", data)
}

self.socket.on("location::share.error") { (data, ack) in
print("location::share.error-->", data)
}


self.socket.on("location::receive") { (data, ack) in

print("location::receive-->", data[0])

}

}

func establishConnection() {
print("in connect socket")
socket.connect()
}

func disconnectSocket() {
socket.disconnect()
print("in disconnect socket")
}

func updateCurrentLocation(myLocation: CLLocation) {

print(myLocation.coordinate.latitude)
print(myLocation.coordinate.longitude)
print(myLocation.course)


let data : Dictionary<String,Any> = [
"latitude": myLocation.coordinate.latitude,
"longitude": myLocation.coordinate.latitude,
"angle": myLocation.course
]
let jsonData = try! JSONSerialization.data(withJSONObject: data)
print(jsonData)
let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)

print((String(describing: jsonString!)))
socket.emit("location::share", "\(String(describing: jsonString!))")

}

最佳答案

socket.emit(Your_Key, with: [Your_Data])

也许这就是您的服务器查找数据的方式!! (发出你的数据数组)。在发出数据之前检查您的套接字状态。

关于ios - Socket.io swift 客户端自动多次重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56977021/

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