Bonjour:在iOS / Objective-C中发布带有服务名称和类型

时间:2015-11-24 15:24:41

标签: objective-c bonjour nsnetservice nsnetservicebrowser

我已经看到NSNetService可以发布Bonjour服务,设置服务名称和服务类型。我这样做:

/**
 Publish this Socket Server on local domain
 @param serviceName String The service name to publish
 @param serviceType String The bonjour service name named as _serviceType._tcp
 */
- (void)publish:(NSString*)serviceName serviceType:(NSString*)serviceType
{

    self.serviceName = serviceName;
    self.serviceType = serviceType;

    self.listenSocket = [[MXMAsyncSocket alloc] initWithDelegate:self];
    NSError *error;
    if (![listenSocket acceptOnPort:0 error:&error])
    {
        [[Logger sharedLogger] error:error
                            withMessage:@"Error starting NetServer"];
        return;
    }

    int port = [listenSocket localPort];

    [[Logger sharedLogger] info:@"NetServer started [%@:%ld]", self.serverName, port];

    int seed = arc4random_uniform(9999);
    self.serverName = [NSString stringWithFormat:@"%@_%@_%d",
                       self.serviceName,
                       self.deviceName,
                       seed];

    // register itself to bonjour service.
    netService = [[NSNetService alloc] initWithDomain:@"local."
                                                  type: [NSString stringWithFormat:@"_%@._tcp", self.serviceType]
                                                  name: self.serverName
                                                  port: port];

    if (!netService)
    {
        [[Logger sharedLogger] warn:@"Failed to enable NetService [%@:%ld]", self.serverName, port];
        [listenSocket disconnect];
        return;
    }

    [netService setIncludesPeerToPeer:YES];
    [netService setDelegate:self];
    [netService publish];
    [netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

}

现在,当我查看像Bonjour Browser by TildeSoft这样的服务浏览器时,我可以看到NetService的发布方式如下:

enter image description here

我可以看到我在服务类型servicename下有命名服务myservicetype._tcp.

那么如何发布名称类似于Bonjour AirTunes _airport._tcp,网络服务我的意思是我只能按类型发布我的服务: myservicetype._tcp.

0 个答案:

没有答案