gpt4 book ai didi

swift - 快速实现socket.io-client

转载 作者:行者123 更新时间:2023-11-30 10:38:29 26 4
gpt4 key购买 nike

我正在尝试快速实现socket.io-client。它没有按预期工作。我无法读取服务器对我的请求的任何响应。我的套接字正在连接,但之后我无法调用事件并获得它们的响应。 Swift 4、Xcode 10

我使用全局类将套接字函数声明为“clsGlobal”,并在不同的 View Controller 中调用套接字函数。

导入基础导入 UIKit导入SocketIO

clsSocket 类:NSObject {

let appdelegate = UIApplication.shared.delegate as! AppDelegate

static var manager = SocketManager(socketURL: URL(string: "my url")!, config: [.log(true), .reconnects(true)])
static var socket = manager.defaultSocket

static func ConnectSocket(){


socket.connect()
print(socket.status)

socket.on("locationsUpdate", callback: { data,ack in
print(data)
let resp = data[0] as! Any
print(resp)
})
}
static func updateLocation(dId:NSNumber, uId:NSNumber, distance:Double){
let datafield : [String:Any] = ["id":dId,
"userid": uId,
"travelledDistance":distance
]
let data = try? JSONSerialization.data(withJSONObject: datafield)
if socket.status == .connected {
socket.emit("updateLocation", data!)
socket.emit("requestLocations")
}

}
static func dogDisconnect(dogid:NSNumber, userid:NSNumber){
let datafield = ["dId":did, "uId":userid]
let dataobj = try? JSONSerialization.data(withJSONObject: datafield)
socket.emit("disconnect", dataobj!)
socket.disconnect()
socket.off("requestLocations")
}

}

如何知道服务器是否已连接以及如何打印服务器的响应?

最佳答案

我已经为套接字类创建了一个全局

import SocketIO

class SocketHelper {

static let shared = SocketHelper()
var socket: SocketIOClient!

let manager = SocketManager(socketURL: URL(string: AppUrls.socketURL)!, config: [.log(true), .compress])

private init() {
socket = manager.defaultSocket
}

func connectSocket(completion: @escaping(Bool) -> () ) {
disconnectSocket()
socket.on(clientEvent: .connect) {[weak self] (data, ack) in
print("socket connected")
self?.socket.removeAllHandlers()
completion(true)
}
socket.connect()
}

func disconnectSocket() {
socket.removeAllHandlers()
socket.disconnect()
print("socket Disconnected")
}

func checkConnection() -> Bool {
if socket.manager?.status == .connected {
return true
}
return false

}

enum Events {

case search

var emitterName: String {
switch self {
case .searchTags:
return "emt_search_tags"
}
}

var listnerName: String {
switch self {
case .search:
return "filtered_tags"
}
}

func emit(params: [String : Any]) {
SocketHelper.shared.socket.emit(emitterName, params)
}

func listen(completion: @escaping (Any) -> Void) {
SocketHelper.shared.socket.on(listnerName) { (response, emitter) in
completion(response)
}
}

func off() {
SocketHelper.shared.socket.off(listnerName)
}
}
}

使用方法

使用此代码连接套接字

SocketHelper.shared.connectSocket { (success) in

}

开始监听事件

SocketHelper.Events.search.listen { [weak self] (result) in
// print(result[0])
}

发出事件

SocketHelper.Events.search.emit(params: <--Your Params-->)

关于swift - 快速实现socket.io-client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369024/

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