0

我已经看到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];

}

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

在此处输入图像描述

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

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

4

0 回答 0