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()
}
}
Above is of course a common piece of code, but what should I implement to respond to a ping from the connected server? I did read several docs but couldn't find anything, any pointers or suggestions.
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?