我正在使用 Socket.IO-Client-Swift','~> 13.1.0'作为我的应用程序。我已经成功地将我的应用程序与套接字服务器连接,但问题是,它多次重新连接。结果,也许我无法向服务器发出任何内容。谁能帮我解决这个问题?
这是我的代码:
class 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])
也许这就是您的服务器查找数据的方式!!(发出数据数组(。在发出数据之前检查套接字状态。
在
调用ON事件之前,您是否尝试过使用socket.off('event'(?