gpt4 book ai didi

ios - Socket.io - 维护套接字连接(Swift)

转载 作者:搜寻专家 更新时间:2023-10-31 23:08:38 25 4
gpt4 key购买 nike

我正在制作一个使用 socket.io 的 iOS 应用程序。场景非常简单,一个人预定了一次冒险,即使用 emit 命令发送数据。之后,我使用了 socket.on 来从服务器获取响应。这在一段时间内工作得很好,但现在我遇到了问题。数据既没有到达服务器,也没有响应返回。我搜索了这个问题,得到的理解是:

在某些时候套接字会断开连接,因此它会停止工作。我什至在发送数据之前使用了 socket.reconnect 但它没有用。

我为 node.js 找到了 forceNew 的参数,但在 swift 中没有找到它的任何替代项。

这是我的代码(与 socket.io 相关):

在 AppDelegate 中:

AppDelegate.socket = SocketIOClient(socketURL: URL(string: "######")!, config: [.log(true), .compress])
AppDelegate.socket.connect ()

在 View Controller 中:

override func viewDidLoad() {
super.viewDidLoad()

socket = AppDelegate.socket

self.socket.on("book-adventure-seeker") {[weak self] data, ack in
print (data)
}
}
@IBAction func bookAdventureTapped(_ sender: Any) {

let jwtToken = UserDefaults.standard.string(forKey: "jwtToken")

let data = ["adventure_id": adventureId, "jwt": jwtToken] as [String : Any]
self.socket.emit("book-adventure", data)
}

日志是:

2017-10-20 09:45:04.665 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws:  has data: false
2017-10-20 09:45:08.541 CuufyPrototype[2591:93618] LOG SocketIOClient: Emitting: 2["book-adventure",{"jwt":"######","adventure_id":"######"}]
2017-10-20 09:45:08.541 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: as type: 2
SocketIOClientStatus
2017-10-20 09:45:22.804 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws: 2["book-adventure",{"jwt":"######","adventure_id":"#####"}] has data: false
2017-10-20 09:45:22.805 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: 2["book-adventure",{"jwt":"#######","adventure_id":"######"}] as type: 4
2017-10-20 09:45:22.807 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws: has data: false
2017-10-20 09:45:22.807 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: as type: 2
2017-10-20 09:45:22.810 CuufyPrototype[2591:93661] ERROR SocketEngine:
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] ERROR SocketIOClient:
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: error with data: [""]
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Starting reconnect
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: reconnect with data: [""]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Trying to reconnect
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: reconnectAttempt with data: [-1]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]

即使我尝试像这样在 AppDelegate 中使用 socket.on:(如官方文档中的示例)

AppDelegate.socket = SocketIOClient(socketURL: URL(string: "#######")!, config: [.log(true), .compress])

AppDelegate.socket.on("book-adventure-seeker") {[weak self] data, ack in
print (data)
}
AppDelegate.socket.connect ()

但它仍然对我不起作用。

更新:有时在发出命令时,Engine is being closed 出现在日志中,但我不知道如何解决这个问题。

最佳答案

在 swift 中将此代码用于 forceNew:

let config : [String: Any] = ["log": true,
"compress": true,
"forcePolling": true,
"forceNew": true]
var socketManager = SocketManager(socketURL: url, config: config)
var socket: SocketIOClient = socketManager.defaultSocket

关于ios - Socket.io - 维护套接字连接(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842764/

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