gpt4 book ai didi

ios - iOS 中的 Websocket 连接问题?

转载 作者:行者123 更新时间:2023-11-28 13:36:35 24 4
gpt4 key购买 nike

我在我的应用程序中使用网络套接字连接。当我尝试建立网络套接字连接但未连接时。我正在使用 Starscream 进行网络套接字连接。我已经尝试使用许多测试 WS Url 进行测试,但没有一个 url 有效。目前我正在模拟器中进行测试。是否存在任何代理问题或防火墙问题?

class ViewController: UIViewController, WebSocketDelegate {
var socket: WebSocket!

override func viewDidLoad() {
super.viewDidLoad()
let urlString = "wss://echo.websocket.org" // testing url

var request = URLRequest(url: URL(string: urlString)!)
request.timeoutInterval = 30
socket = WebSocket(request: request)
socket.delegate = self
socket.pongDelegate = self as? WebSocketPongDelegate
socket.connect()
}

// MARK: Websocket Delegate Methods.
// Never call this method
func websocketDidConnect(socket: WebSocketClient) {
print("websocket is connected")
}

func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
if let e = error as? WSError {
print("websocket is disconnected: \(e.message)")
} else if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}

func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
print("Received text: \(text)")
}

func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
print("Received data: \(data.count)")
}

}

最佳答案

如果您还没有这样做,请检查几件事:

  • 请记住,苹果默认会阻止所有非 https 网址。在 - 中将任意负载更改为 YESinfo.plist -> 应用传输安全设置> 允许任意加载

  • 设置self.socket.selfSignedSSL = true

关于ios - iOS 中的 Websocket 连接问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540665/

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