使用 Network.framework 检测客户端与 UDP 断开连接

Emm*_*dra 6 swift server-side-swift network.framework

我试图确定使用 Network.framework 时 UDP 客户端何时停止向服务器发送数据包

我构建了一个小示例,演示服务器在.cancelled客户端连接取消时无法更改状态。

客户端示例:

import Foundation
import Network

func sendMessage(on connection: NWConnection) {
    connection.send(content: "hello".data(using: .utf8), completion: .contentProcessed({error in
        if let error = error {
            print("error while sending hello: \(error)")
            return

        }

        connection.receiveMessage {data, context, isComplete, error in
            if let error = error {
                print("error while receiving reply: \(error)")
                return

            }

            connection.cancel()

        }

    }))
}

var connection: NWConnection = {
    let connection = NWConnection(
        to: .service(
            name: "Hello",
            type: "_test._udp",
            domain: "local",
            interface: nil
        ),
        using: .udp
    )

    connection.stateUpdateHandler = {newState in
        switch newState {
        case .ready:
            sendMessage(on: connection)
        case .failed(let error):
            print("client failed with error: \(error)")
        case .cancelled:
            print("Cancelled connection")
        default:
            break
        }
    }

    return connection
}()

connection.start(queue: DispatchQueue(label: "test"))

RunLoop.main.run()
Run Code Online (Sandbox Code Playgroud)

服务器示例:

import Foundation
import Network

func receive(on connection: NWConnection) {
    connection.receiveMessage { (data, context, isComplete, error) in
        if let error = error {
            print(error)
            return

        }

        connection.send(content: "world".data(using: .utf8), completion: .contentProcessed({error in
            if let error = error {
                print("error while sending data: \(error)")
                return

            }

        }))

        receive(on: connection)

    }

}

var listener: NWListener = {
    let listener = try! NWListener(using: .udp)

    listener.service = NWListener.Service(name: "Hello", type: "_test._udp", domain: nil, txtRecord: nil)
    listener.newConnectionHandler = {newConnection in
        newConnection.stateUpdateHandler = {newState in
            switch newState {
            case .ready:
                receive(on: newConnection)
            case .failed(let error):
                print("client failed with error: \(error)")
            case .cancelled:
                print("Cancelled connection")
            default:
                break
            }
        }

        newConnection.start(queue: DispatchQueue(label: "new client"))


    }
    return listener
}()

listener.start(queue: DispatchQueue(label: "test"))

RunLoop.main.run()
Run Code Online (Sandbox Code Playgroud)

当服务器运行时运行客户端时,客户端发送和接收一个数据包,然后被取消。客户端打印Connection cancelled. 但是,服务器上 NWConnection 的状态不会更改,并且connection.receiveMessage当没有从客户端读取数据时会静默失败。

我希望服务器连接的状态发生变化或receiveMessage调用其完成处理程序,尽管没有数据存在(data毕竟Data?

因此,我不确定在 Network.framework 上使用 UDP 服务器时如何检测客户端何时停止发送数据包。我应该如何检测“断开连接”的客户端?

hot*_*aw2 4

UDP 服务器不会通过网络获取有关 UDP 客户端是否已断开连接或离开的信息,除非客户端可能明确发送某种有关其断开状态的附加消息(通过 UDP、TCP 或其他侧通道)。因此,没有什么可以改变 NWConnection 状态(除了服务器本身的某种问题)。

也许服务器可以在经过一些商定或协商的超时时间而没有某种活动的情况下假设断开连接。或者数据包的数量、数据的字节数。等等并关闭连接本身。