NetService 发布失败
NetService failing to publish
我正在尝试做一个简单的测试,在 Swift 本地发布一个 bonjour 服务。我通过一个简单的单视图应用程序来执行此操作,该应用程序仅创建一个 netService 对象并尝试发布它。当我 运行 应用程序无法发布时,代理调用了 didNotPublish 函数,生成错误
code -72004 ("An required argument was not provided when initializing the NSNetService instance").
我不知道缺少的参数可能是什么,因为我已经指定了每个域、类型、名称和端口。
class ViewController: NSViewController, NetServiceDelegate {
var netService : NetService?
override func viewDidLoad() {
super.viewDidLoad()
//initialize the NetService object
self.netService = NetService(domain: "local.", type: "testService._tcp.", name: "netServiceTest", port: Int32(80))
//assing NetService delegate to ViewController object
self.netService!.delegate = self
//publish it
self.netService!.publish()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
//netservice delegate functions
func netService(_ sender: NetService, didNotPublish errorDict: [String : NSNumber]) {
print("uh oh, could not publish netService. domain:\(netService!.domain) type:\(netService!.type) name:\(netService!.name) port:\(netService!.port)")
print("error code:\(errorDict)")
}
func netServiceDidPublish(_ sender: NetService) {
print("netService published.")
}
func netServiceDidStop(_ sender: NetService) {
print("netService stopped.")
}
func netServiceWillPublish(_ sender: NetService) {
print("Service will publish, apparently")
}
}
您的类型设置不正确。您需要在类型前加上下划线。
所以它应该是这样的:"_testService._tcp."
type
The network service type.
type must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, as opposed to hosts, prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string "_http._tcp.". Note that the period character at the end of the string, which indicates that the domain name is an absolute name, is required.
我正在尝试做一个简单的测试,在 Swift 本地发布一个 bonjour 服务。我通过一个简单的单视图应用程序来执行此操作,该应用程序仅创建一个 netService 对象并尝试发布它。当我 运行 应用程序无法发布时,代理调用了 didNotPublish 函数,生成错误
code -72004 ("An required argument was not provided when initializing the NSNetService instance").
我不知道缺少的参数可能是什么,因为我已经指定了每个域、类型、名称和端口。
class ViewController: NSViewController, NetServiceDelegate {
var netService : NetService?
override func viewDidLoad() {
super.viewDidLoad()
//initialize the NetService object
self.netService = NetService(domain: "local.", type: "testService._tcp.", name: "netServiceTest", port: Int32(80))
//assing NetService delegate to ViewController object
self.netService!.delegate = self
//publish it
self.netService!.publish()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
//netservice delegate functions
func netService(_ sender: NetService, didNotPublish errorDict: [String : NSNumber]) {
print("uh oh, could not publish netService. domain:\(netService!.domain) type:\(netService!.type) name:\(netService!.name) port:\(netService!.port)")
print("error code:\(errorDict)")
}
func netServiceDidPublish(_ sender: NetService) {
print("netService published.")
}
func netServiceDidStop(_ sender: NetService) {
print("netService stopped.")
}
func netServiceWillPublish(_ sender: NetService) {
print("Service will publish, apparently")
}
}
您的类型设置不正确。您需要在类型前加上下划线。
所以它应该是这样的:"_testService._tcp."
type
The network service type. type must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, as opposed to hosts, prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string "_http._tcp.". Note that the period character at the end of the string, which indicates that the domain name is an absolute name, is required.