我正在尝试使用红蜘蛛连接到 websocket,它似乎正在连接,但我没有收到任何东西,也无法发送任何东西。
我的代码或多或少与红蜘蛛上的示例代码相同
override func viewDidLoad() {
super.viewDidLoad()
let start = ""
let baseUrl = ""
let ending=""
let token = ""
let urlString = "\(start)\(baseUrl)\(ending)"
var request = URLRequest(url: URL(string: urlString)!)
request.addValue(token, forHTTPHeaderField: "x-token")
request.timeoutInterval = 5
socket = WebSocket(request: request)
socket.delegate = self
socket.connect()
}
func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected(let headers):
isConnected = true
print("websocket is connected: \(headers)")
case .disconnected(let reason, let code):
isConnected = false
print("websocket is disconnected: \(reason) with code: \(code)")
case .text(let string):
print("Received text: \(string)")
case .binary(let data):
print("Received data: \(data.count)")
case .ping(_):
print("PING")
break
case .pong(_):
print("PONG")
break
case .viabilityChanged(_):
break
case .reconnectSuggested(_):
break
case .cancelled:
isConnected = false
case .error(let error):
isConnected = false
handleError(error)
}
}
func handleError(_ error: Error?) {
if let e = error as? WSError {
print("websocket encountered an error: \(e.message)")
} else if let e = error {
print("websocket encountered an error: \(e.localizedDescription)")
} else {
print("websocket encountered an error")
}
}
@IBAction func Write(_ sender: UIButton) {
socket.write(string: "hello there!")
}
@IBAction func Stop(_ sender: UIButton) {
if isConnected {
socket.disconnect()
} else {
socket.connect()
}
}
}
我在 xcode 终端中收到此消息
2021-08-05 11:28:50.648012+0200 websocplayground[12123:4021753] [connection] nw_endpoint_handler_set_adaptive_read_handler [C1.1 34.232.132.218:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: pdp_ip0, ipv4, ipv6, dns, expensive)] unregister notification for read_timeout failed
2021-08-05 11:28:50.648528+0200 websocplayground[12123:4021753] [connection] nw_endpoint_handler_set_adaptive_write_handler [C1.1 34.232.132.218:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: pdp_ip0, ipv4, ipv6, dns, expensive)] unregister notification for write_timeout failed
websocket is connected: ["X-Request-Id": "", "Upgrade": "websocket", "Sec-WebSocket-Accept": "", "Connection": "upgrade", "Strict-Transport-Security": "max-age=1296000", "Access-Control-Allow-Origin": "*", "Date": "Thu, 05 Aug 2021 09:28:50 GMT"]
我不确定出了什么问题,谷歌搜索答案没有给我任何东西有没有人经历过同样的事情并找到了解决方案?