I use the Apple-provided URLSessionWebSocketTask
to set up a websocket. I receive, send messages and can send a ping.
But how should I receive and respond to a ping coming from a ws server? I have to work with a server that sends a ping to clients to check if they still are alive.
If I receive something from the server, how can I recognise a ping from that server? Is there some keycode in a received message?
func receive() {
webSocketTask.receive { result in
print(result)
switch result {
case .failure(let error):
print("Failed to receive message: (error)")
....
case .success(let message):
switch message {
case .data(let data):
print("(data)")
....
case .string(let text):
print("(text)")
.....
@unknown default:
fatalError()
}
}
receive()
}
}
当然,这只是一部共同的法典,但我应执行什么来应对连接的服务器的拼凑? 我读了几个书,但 could没有发现任何东西、任何观点或建议。
edit:
I did found the following OpCode 0x9
and 0xA
. Are those Ping and Pong? Could I send a 0x9
as ping and for pong a 0xA
? If I try this as text message but I get no reaction. Should it be a frame? So what did I do I wrong?