2
我正在使用socket.io的iOS應用程序。該場景非常簡單,一個人預訂冒險,即發送數據使用發出命令。之後,我使用了socket.on以便從服務器獲得響應。這在一段時間內工作完美,但現在我面臨着問題。數據無法到達服務器,也沒有迴應。 我搜索了關於這個問題,我得到的理解是:Socket.io - 維護套接字連接(Swift)
有一點在斷開連接,因此它停止工作。我甚至在發送數據之前使用了socket.reconnect
,但它不起作用。
我找到了node.js的forceNew
的一個參數,但是在swift中沒有找到任何替代方法。
這裏是我的代碼(以socket.io相關):
在AppDelegate中:
AppDelegate.socket = SocketIOClient(socketURL: URL(string: "######")!, config: [.log(true), .compress])
AppDelegate.socket.connect()
在視圖控制器:
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]
即使我試圖使用socket.on
在AppDelegate的是這樣的:(爲例,從官方文檔中)
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
出現在日誌中,但我沒有得到任何線索如何解決此問題。