iOS 中的网络套接字连接问题



我正在我的应用程序中使用网络套接字连接。当我尝试建立 Web 套接字连接时,它没有连接。我正在使用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 -> 应用传输安全设置>允许任意加载

  • Set self.socket.selfSignedSSL = true

相关内容

  • 没有找到相关文章

最新更新