2

在http://188.120.235.97:8082/有一个 Traccar 服务器,尝试使用 Swift 库 Starscream ( https://github.com/daltoniam/Starscream ) 连接 websocket。有我的代码:

func connect(_ token: String) {

    var urlString = "\(self.serverURL)api/socket"
    urlString = urlString.replacingOccurrences(of: "http", with: "ws")

    self.socket = WebSocket(url: URL(string: urlString)!, protocols: ["chat"])

    guard let socket = self.socket else { return }

    socket.headers["Cookie"] = "JSESSIONID=\(token)"
    socket.headers["Sec-WebSocket-Key"] = "\(token)"
    socket.headers["Sec-WebSocket-Protocol"] = "chat"
    socket.headers["Sec-WebSocket-Version"] = "13"
    socket.headers["Host"] = self.serverURL
    socket.headers["Upgrade"] = "websocket"
    socket.headers["Connection"] = "Upgrade"
    socket.headers["Origin"] = self.serverURL
    socket.headers["Sec-WebSocket-Accept"] = "application/json, text/html"

    socket.delegate = self
    socket.connect()


    print("isConnected: \(socket.isConnected)")
    print("socket: \(socket)")
    print("headers: \(socket.headers)")
}

什么也没发生,websocket 的状态是 CONNECTING,从不打开。在调用 socket.connect 时,使用 Fiddler 也看不到任何 GET 请求...它在 iOS 模拟器中是否有效?

4

0 回答 0