我正在尝试使用swift中的starscream Web套接字连接到我的AWS EC2 linux实例,但每次运行我的代码时,套接字都无法连接。
这是我用来设置和连接套接字的代码:
var socket = WebSocket(url: URL(string: "ws://My-ec2-instance:8085")!, protocols: ["test"])
func connect() {
socket.disableSSLCertValidation = true
socket.delegate = self as WebSocketDelegate
socket.connect()
}
我有这两种委托方法:
func websocketDidConnect(socket: WebSocket) {
print("websocket is connected")
}
func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
print("websocket is disconnected: \(String(describing: error?.localizedDescription))")
}
但每当我执行connect()
函数时,都会调用websocketDidDisconnect
方法,并在控制台中打印:websocket is disconnected: nil