我正在尝试在 AppDelegate
的 applicationWillTerminate 中发出套接字事件。我使用过socket.io
。
我相信我能够成功发出该事件。 以下是我在控制台中收到的日志:
2018-10-08 20:29:45.663856+0530 SOCKET_POC[4718:303986] LOG SocketIOClient{/}: Emitting: 23["device-disconnect","{\"QrcodeID\":\"2a758b00-314f-d69d-a10b-24fca56693ed\",\"UniqueID\":\"E15C087D-626F-4F10-B0B3-7567DA76EF51\"}"], Ack: false 2018-10-08 20:29:49.561608+0530 SOCKET_POC[4718:304214] LOG SocketEngine: Writing ws: 23["device-disconnect","{\"QrcodeID\":\"123\",\"UniqueID\":\"123\"}"] has data: false 2018-10-08 20:29:49.561675+0530 SOCKET_POC[4718:304214] LOG SocketEngineWebSocket: Sending ws: 23["device-disconnect","{\"QrcodeID\":\"123\",\"UniqueID\":\"123\"}"] as type: 4
请找到下面的代码:
func applicationWillTerminate(_ application: UIApplication) {
print(SocketHandler.instance?.socket.status)
if let uniqueId = UserDefaults.standard.value(forKey: Constants.USER_DEFAULT_KEYS.UINQUE_ID) as? String {
CommonFunctions().emitResultOnDeviceDisconnect(qrcode: "\(Variables.QRCodeID)", uniqueId: uniqueId)
print(SocketHandler.sharedInstance().socket.status)
}
我什至打印了我的套接字的状态,它总是打印已连接。
但是当我在服务器(后端)看到日志时,它们根本没有收到我发出的套接字事件。
最佳答案
在你的 AppDelegate.Swift 中, 将“applicationDidEnterBackground(_ application: UIApplication)”更改为以下代码==>
func applicationDidEnterBackground(_ application: UIApplication) {
var bgTask: UIBackgroundTaskIdentifier = 0;
bgTask = application.beginBackgroundTask(withName:"MyBackgroundTask", expirationHandler: {() -> Void in
print("The task has started")
application.endBackgroundTask(bgTask)
bgTask = UIBackgroundTaskInvalid
})
}
并保持“applicationWillTerminate”逻辑不变。
关于ios - 在 applicationWillTerminate 中发出套接字事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52716338/