我在我的应用程序中使用了Web套接字连接。当我尝试建立Web套接字连接并且未连接时。我正在使用Starscream建立Web套接字连接。我已经尝试过使用许多测试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)")
}
}
答案 0 :(得分:1)
如果您没有做这些事情,请检查:
请记住,默认情况下,Apple阻止所有非https网址。在-中将任意载荷更改为YES info.plist->应用程序传输安全设置>允许任意加载
设置 self.socket.selfSignedSSL = true