0

我正在尝试像这样连接到我的本地主机套接字(工作正常):

import Starscream    

class ViewController: UIViewController {

let conn = ConnectionManager()


override func viewDidLoad() {
    super.viewDidLoad()

    conn.startSession()
}

}


class ConnectionManager {

    let socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)

    func startSession(){
        socket.delegate = self
        socket.connect()
    }
}

extension ConnectionManager: WebSocketDelegate{
    func websocketDidConnect(socket: WebSocket) {
        print("connected")
    }

    func websocketDidDisconnect(socket: WebSocket, error: NSError?) {

    }

    func websocketDidReceiveMessage(socket: WebSocket, text: String) {
        print("text")
    }

    func websocketDidReceiveData(socket: WebSocket, data: Data) {

    }

//put the delegate methods here

}

但是在我的服务器中,它不会通知我已经建立了新的连接。我究竟做错了什么?

4

1 回答 1

0

尝试这个:

import Starscream
class ConnectionManager {

let socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)

func startSession(){
     socket.delegate = self
     socket.connect()
}

}

extension ConnectionManager: WebSocketDelegate{
        //put the delegate methods here
}
于 2017-08-16T04:18:49.653 回答