【问题标题】:Socket.io - Maintaining socket connection (Swift)Socket.io - 维护套接字连接 (Swift)
【发布时间】:2017-10-20 05:16:56
【问题描述】:

我正在制作一个使用 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 ()

在 ViewController 中:

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 会出现在日志中,但我不知道如何解决这个问题。

【问题讨论】:

    标签: ios swift socket.io


    【解决方案1】:

    在 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
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 2014-08-05
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多