gpt4 book ai didi

ios - swift socket io 连接到服务器,但正在释放管理器和引擎

转载 作者:搜寻专家 更新时间:2023-11-01 07:04:55 25 4
gpt4 key购买 nike

我正在尝试制作一个连接到我的节点后端 socket.io 服务器的 super 基本的 swift 应用程序,但是当我运行以下代码时,出现了一个很长的错误,如下所示。在后端,它表示该应用已连接到服务器,但是当我尝试发出任何事件时,我收到另一个错误,该错误也在下方。

swift代码:

import UIKit
import SocketIO

class ViewController: UIViewController {

@IBOutlet weak var messagesBody: UITextView!
@IBOutlet weak var newMessageInput: UITextField!
@IBOutlet weak var newMessageSubmit: UIButton!

var socket:SocketIOClient!

override func viewDidLoad() {
super.viewDidLoad()

let manager = SocketManager(socketURL: URL(string: "http://localhost:3000/chat.html?name=developer&room=test")!, config: [.log(true), .compress])
self.socket = manager.socket(forNamespace: "/")

establishSocketConnection()

newMessageSubmit.addTarget(self, action: #selector(sendNewMessage), for: .touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func establishSocketConnection() {
self.socket.connect()
self.socket.on("connect") { ( dataArray, ack) -> Void in
print("connected to external server")
}
}

func cloesSocketConnection() {
self.socket.disconnect()
}

@objc func sendNewMessage() {
let newMessageContent = newMessageInput.text!
print("Sending message..." + newMessageContent)
self.socket.emit("createMessage", newMessageContent)
}
}

主要错误:

2018-02-07 17:53:33.809954-0500 chat-app[78163:6477441] [MC] Lazy 
loading NSBundle MobileCoreServices.framework
2018-02-07 17:53:33.811229-0500 chat-app[78163:6477441] [MC] Loaded
MobileCoreServices.framework
2018-02-07 17:53:33.915820-0500 chat-app[78163:6477441] LOG
SocketIOClient{/swift}: Handling event: statusChange with data:
[connecting]
2018-02-07 17:53:33.916535-0500 chat-app[78163:6477441] LOG
SocketIOClient{/swift}: Joining namespace /swift
2018-02-07 17:53:33.918305-0500 chat-app[78163:6477441] LOG
SocketManager: Tried connecting socket when engine isn't open.
Connecting
2018-02-07 17:53:33.918627-0500 chat-app[78163:6477441] LOG
SocketManager: Adding engine
2018-02-07 17:53:33.921886-0500 chat-app[78163:6477498] LOG
SocketEngine: Starting engine. Server: http://localhost:3000/chat.html?
name=developer&room=test
2018-02-07 17:53:33.922020-0500 chat-app[78163:6477498] LOG
SocketEngine: Handshaking
2018-02-07 17:53:33.922060-0500 chat-app[78163:6477441] LOG
SocketIOClient{/swift}: Adding handler for event: connect
2018-02-07 17:53:33.922966-0500 chat-app[78163:6477441] LOG
SocketManager: Manager is being released
2018-02-07 17:53:33.924349-0500 chat-app[78163:6477498] LOG
SocketEnginePolling: Doing polling GET
http://localhost:3000/socket.io/?transport=polling&b64=1
2018-02-07 17:53:34.014299-0500 chat-app[78163:6477498] LOG
SocketEnginePolling: Got polling response
2018-02-07 17:53:34.016531-0500 chat-app[78163:6477498] LOG
SocketEnginePolling: Got poll message:
97:0{"sid":"DRRfs3CyuXRDeYVIAAAA","upgrades":
["websocket"],"pingInterval":25000,"pingTimeout":60000}
2018-02-07 17:53:34.022590-0500 chat-app[78163:6477498] LOG
SocketEngine: Got message: 0{"sid":"DRRfs3CyuXRDeYVIAAAA","upgrades":
["websocket"],"pingInterval":25000,"pingTimeout":60000}
2018-02-07 17:53:34.045912-0500 chat-app[78163:6477498] LOG
SocketEngine: Writing poll: has data: false
2018-02-07 17:53:34.046635-0500 chat-app[78163:6477498] LOG
SocketEnginePolling: Sending poll: as type: 2
2018-02-07 17:53:34.048335-0500 chat-app[78163:6477498] LOG
SocketEnginePolling: Created POST string: 1:2
2018-02-07 17:53:34.049790-0500 chat-app[78163:6477498] LOG
SocketEnginePolling: POSTing
2018-02-07 17:53:34.050572-0500 chat-app[78163:6477498] LOG
SocketEngine: Engine is being released

第二个错误:

2018-02-07 17:53:37.918418-0500 chat-app[78163:6477441] LOG 
SocketIOClient{/swift}: Handling event: error with data: ["Tried
emitting createMessage when not connected"]

最佳答案

您应该将 SocketManager 声明为存储属性,否则将在 viewDidLoad 之后立即释放。

Sockets created through the manager are retained by the manager. So at the very least, a single strong reference to the manager must be maintained to keep sockets alive.

因此您可以使用以下内容:

class ViewController:UIViewController {
//... stuff here ...//
var manager:SocketManager?

override func viewDidLoad() {
super.viewDidLoad()
self.manager = SocketManager(socketURL: URL(string: "http://localhost:3000/chat.html?name=developer&room=test")!, config: [.log(true), .compress])
}

此外,请更改以下顺序:

func establishSocketConnection() {
self.socket.on("connect") { ( dataArray, ack) -> Void in
print("connected to external server")
}
self.socket.connect()
}

关于ios - swift socket io 连接到服务器,但正在释放管理器和引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48675629/

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