English 中文(简体)
快速5 - URLSessionWebSocketTask - response to ping from服务器 with/67/ pong
原标题:Swift5 - URLSessionWebSocketTask - response to ping from server with client pong

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?

问题回答

它进行了一些挖掘,但你必须在<条码>上打上“奥色编码”栏。 您的数据应当反映发送到的信息。

  let metadata = NWProtocolWebSocket.Metadata(opcode: .pong)
  let context = NWConnection.ContentContext(identifier: "context", metadata: [metadata])
    
  client.send(content: data, contentContext: context, isComplete: true, completion: .contentProcessed({ error in
        if let error = error {
            print(error.localizedDescription)
        } else {
            // no-op
        }
    }))

页: 1





相关问题
Client ping to servers

I am wondering what would the best way to let visitors of a website ping various remote servers. Example: I am a visitor, I choose from a lsit of locations Paris, France and the script would ping from ...

How to do a true Java ping from Windows?

I have a device on a network that I am attempting to ping through my Java program. Through my windows command prompt, I can ping the device address fine and do a tracert on the address fine. Online, ...

Ping a network using threads and testing it

I am trying to ping two different networks with thread. I am able to get the response I want but I want to convert it into a test. I have the code that I have tried below but the test runner says that ...

Threading issues using Ping to map active IPs - C#

I am trying to create a simple Network Tool to ping all possible IPs on your local subnet and provide a list of such IPs in a DataGridView. I am new to having to consider threading which is a good ...

how to use ping in a script

I want a bash script that ll do: for c in computers: do ping $c if ping is sucessfull: ssh $c check something done If I only do ssh and the computer is iresponsive, it takes forever ...

热门标签