我一直在尝试连接到本地服务器但没有成功。我的代码如下 -
class SocketManager: NSObject, WebSocketDelegate {
var socket: WebSocket!
override init() {
super.init()
self.socket = WebSocket(url: NSURL(string: "ws://localhost:9292/")!)
self.socket.delegate = self
print("TRYING TO CONNECT")
self.socket.connect()
print("DONE TRYING")
}
func websocketDidConnect(ws: WebSocket) {
print("websocket is connected")
}
func websocketDidDisconnect(ws: WebSocket, error: NSError?) {
print("websocket is disconnected: \(error?.localizedDescription)")
}
func websocketDidReceiveMessage(ws: WebSocket, text: String) {
print("Received text: \(text)")
}
func websocketDidReceiveData(ws: WebSocket, data: NSData) {
print("Received data: \(data.length)")
}
func websocketDidReceivePong(socket: WebSocket) {
print("Got pong!")
}
}
打印语句“TRYING TO CONNECT”和“DONE TRYING”都出现在日志中,但似乎没有调用任何委托方法。
我不确定这里可能有什么问题。
任何帮助表示赞赏。