Hi Guys,
im trying deep dive into the new Network.framework. impossible to find any tutorials about...
So i wanted to test the example from apple which was shown on the WWDC2018
https://developer.apple.com/videos/play/wwdc2018/715/
But it's not working. has anyone tried out the example from the video ?
I implemented the UDP Server Class and during test, it's throwing the error badParam -65540 ?!
Has anyone a working example for me ?!
import Foundation
import Network
class UDPServer {
var listner: NWListener
var queue = DispatchQueue(label: "UDP.Server")
required init() {
self.listner = try! NWListener(using: .udp)
}
func start() {
self.createListner()
self.connectionHandler()
self.stateHandler()
self.listner.start(queue: self.queue)
}
func receive(on connection: NWConnection) {
connection.receiveMessage { (data, context, isComplete, error) in
if let _ = data {
print("Received Data")
}
if error == nil {
self.receive(on: connection)
}
}
}
}
extension UDPServer {
private func createListner() {
self.listner.service = NWListener.Service.init(type: "_udp_.test")
self.listner.serviceRegistrationUpdateHandler = { service in
switch service {
case .add(let endpoint):
switch endpoint {
case .service(let name, let type, let domain, let interface):
print("name:\(name), type:\(type), domain:\(domain), interface:\(String(describing: interface))")
break
case .hostPort(_, _):
break
case .unix(_):
break
}
case .remove(_):
break
}
}
}
private func connectionHandler() {
self.listner.newConnectionHandler = { connection in
connection.start(queue: self.queue)
self.receive(on: connection)
}
}
private func stateHandler() {
self.listner.stateUpdateHandler = { state in
switch state {
case .ready:
print("connection ready, \(String(describing: self.listner.port))")
case .failed(let error):
print("failed start listening with error: \(error)")
case .setup:
break
case .waiting(_):
break
case .cancelled:
break
}
}
}
}