Socket.io - 如何保持Socket连接 (Swift)

3
我正在制作一款iOS应用程序,并使用socket.io。场景非常简单,一个人预订冒险活动,即使用emit命令发送数据。之后,我使用socket.on来从服务器获取响应。这段时间它一直工作得很好,但现在我遇到了问题。数据既没有到达服务器,也没有响应返回。
我搜索了关于这个问题的信息,我的理解是:
有些地方socket断开连接,因此停止工作。我甚至在发送数据之前使用了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]

我尝试在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 ()

但是对我来说还是没有用。

更新:有时候在执行emit命令时,日志中会出现引擎正在关闭的提示,但我不知道如何解决这个问题。

1个回答

0

在 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接