在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模拟器中有效吗?