ios - Multipeer Connectivity 数据仅以一种方式发送,即使两个设备都已连接

标签 ios objective-c multipeer-connectivity

我目前有两台设备通过多对等连接框架相互连接。连接后,每个设备都会立即相互发送一个 nsdata 数据包,在第一个实例中,数据将包含声音和图像。这似乎工作正常,并且两个用户都收到了相关数据。

用户现在可以从 View 中相互选择并发送另一位数据。但是,我发现如果主持人(发送邀请的客户)尝试发送更多数据,控制台中会显示以下错误。

 Peers (
        PeerName
    ) not connected

但是如果客户端(受邀者)发送数据,它会按预期到达主机。特别有趣的是我在以下位置有 NSLogs:

-(void)peerDidChangeStateWithNotification:(NSNotification *)notification

当断开连接发生时触发的方法,没有发生断开连接并且设备相互连接。我的代码在下面,有人知道为什么会发生这种情况吗?

#pragma mark Send Buzz Notification
#pragma mark -
-(void)sendBuzzNotification{

    // create the user (remove the image to lower size)
    LocalUserClass *withoutImage = [[LocalUserClass alloc]initWithName:[[[GlobalData sharedGlobalData]localUser]personName] personImage:nil personSound:[[[GlobalData sharedGlobalData]localUser]personSound] personPeerId:nil];

    // send the users sound and name to the other peer/s
    NSData *dataToSend = [NSKeyedArchiver archivedDataWithRootObject:withoutImage];
    NSError *error;

    // skip if no users are selcted
    if ([selectedUsers count] == 0) {
        return;
    }
    else{
        // create the array of peer Ids from selected users
        NSMutableArray *tempArray = [[NSMutableArray alloc]init];

        for (LocalUserClass *object in selectedUsers) {

            // add the peer id
            MCPeerID *theId = object.personPeerId;
            [tempArray addObject:theId];
        }
        NSArray *theUsers = [[NSArray alloc]init];
        theUsers = tempArray;

    // send the data
    [appDelegate.mcManager.session sendData:dataToSend
                                    toPeers:theUsers
                                   withMode:MCSessionSendDataReliable
                                      error:&error];

    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    }

    // vibrate the device to show its been sent
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
}
#pragma mark Peer Connected State Checker
#pragma mark -
-(void)peerDidChangeStateWithNotification:(NSNotification *)notification{

    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
    //NSLog(@"%@",peerID.displayName);
    MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];
    LocalUserClass *theUser = [[LocalUserClass alloc]initWithName:peerID.displayName personImage:nil personSound:nil personPeerId:peerID];
    //NSLog(@"the user name is: %@ the user sound is: %@ the user peer id is %@",theUser.personName,theUser.personSound,theUser.personPeerId);

    if (state != MCSessionStateConnecting) {

        if (state == MCSessionStateConnected) {

            // add the user
            [connectedUsers addObject:theUser];
            // send the users details to the newly connected peer (profile pic and sound)
            [self sendUserDetailsToPeer:peerID];

        }
        else if (state == MCSessionStateNotConnected){

            // do we have connections
            if ([connectedUsers count] > 0) {

                // get the user to remove
                NSInteger thePeerIndex = 0;
                NSInteger thePeerIndexSelectedUsers = 0;

                for (LocalUserClass *object in connectedUsers) {
                    if (object.personPeerId == theUser.personPeerId) {
                        thePeerIndex = [connectedUsers indexOfObject:object];
                        [connectedUsers removeObjectAtIndex:thePeerIndex];
                    }
                }
                for (LocalUserClass *user in selectedUsers) {
                    if (user.personPeerId == theUser.personPeerId) {
                    thePeerIndexSelectedUsers = [selectedUsers indexOfObject:user];
                    [selectedUsers removeObjectAtIndex:thePeerIndexSelectedUsers];
                    }
                }
            }
        }
    }

    // push to main queue for speedy response
    dispatch_async(dispatch_get_main_queue(), ^(void) {

        // create user playback devices
        [playbackManager createUserAudioPlayers:connectedUsers withSound:nil];

        [collView reloadData];
        [self populateUserBox];

        BOOL peersExist = ([[appDelegate.mcManager.session connectedPeers] count] == 0);
        //NSLog(@"PEER COUNT IS %lu",(unsigned long)[[appDelegate.mcManager.session connectedPeers] count]);
        [disconnectButton setEnabled:!peersExist];

        if ([disconnectButton isEnabled]) {
            [disconnectButton setBackgroundColor:[UIColor colorWithRed:(51/255.0) green:(202/255.0) blue:(168/255.0) alpha:1.0]];
        }
        else{
            [disconnectButton setBackgroundColor:[UIColor colorWithRed:(107/255.0) green:(107/255.0) blue:(107/255.0) alpha:1.0]];
        }

    });

}

#pragma mark Peer Received Data
#pragma mark -
-(void)didReceiveDataWithNotification:(NSNotification *)notification{

    NSData *receivedData = [[notification userInfo] objectForKey:@"data"];
    receivedUser = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];

    CGImageRef cgref = [receivedUser.personImage CGImage];
    CIImage *cim = [receivedUser.personImage CIImage];

     // are we getting a whole user or just the user sound and name (check for no image)
    if (cim == nil && cgref == NULL)
    {
        // recieve the data (TODO probably needs to be run in the background)
        [self performSelectorOnMainThread:@selector(recieveBuzzNotification) withObject:nil waitUntilDone:YES];
    }
    else{
        // recieve the data (TODO probably needs to be run in the background)
       [self performSelectorOnMainThread:@selector(receivedUserProfile) withObject:nil waitUntilDone:YES];
    }
}

更新:主机似乎在初始数据传输后失去与客户端的连接:

[self sendUserDetailsToPeer:peerID];

但是断开连接状态不会触发任何东西,客户端仍然可以向主机发送数据。这可以通过注销连接的 session 看到:

session.connectedPeers

编辑:来自 View Controller 的附加代码:

#pragma mark Create Session
#pragma mark -
-(void)setupSession{

    // create session and peer with details from the user
    [appDelegate.mcManager setupPeerAndSessionWithDisplayName:[[[GlobalData sharedGlobalData]localUser]personName]];
    [appDelegate.mcManager advertiseSelf:visibleSwitch.isOn];
    appDelegate.mcManager.browser.delegate = self;

    // set global user peerId
    [[[GlobalData sharedGlobalData]localUser]setPersonPeerId:appDelegate.mcManager.peerIDGlobal];

    // setup notification to track the user info of connected peers
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(peerDidChangeStateWithNotification:)
                                                 name:@"MCDidChangeStateNotification"
                                               object:nil];
    // notifcation to track recieved data
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveDataWithNotification:)
                                                 name:@"MCDidReceiveDataNotification"
                                               object:nil];
}

和 MCManager:

-(id)init{
    self = [super init];

    if (self) {
        peerIDGlobal = nil;
        session = nil;
        browser = nil;
        advertiser = nil;
    }

    return self;
}

//----CUSTOM---------------------------------------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------------------------------------------------//
//----PUBLIC---------------------------------------------------------------------------------------------------------------//
#pragma mark Setup Peer and Session
#pragma mark -
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{

    peerIDGlobal = [[MCPeerID alloc] initWithDisplayName:[[[GlobalData sharedGlobalData]localUser]personName]];
    session = [[MCSession alloc] initWithPeer:peerIDGlobal];
    session.delegate = self;
}

#pragma mark Setup Browser
#pragma mark -
-(void)setupMCBrowser{
    browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:session];
}

#pragma mark Setup Advertiser
#pragma mark -
-(void)advertiseSelf:(BOOL)shouldAdvertise{

    if (shouldAdvertise) {
        advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
                                                           discoveryInfo:nil
                                                                 session:session];
        [advertiser start];
    }
    else{
        [advertiser stop];
        advertiser = nil;
    }
}


//----DELEGATES------------------------------------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------------------------------------------------//
#pragma mark Session delegate
#pragma mark -
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{

    // create the dict with connected user info
    NSDictionary *dict = @{@"peerID": peerID,
                           @"state" : [NSNumber numberWithInt:state]
                           };

    // post the notification for tracking
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification"
                                                        object:nil
                                                      userInfo:dict];
}


-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{

    NSDictionary *dict = @{@"data": data,
                           @"peerID": peerID
                           };

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification"
                                                        object:nil
                                                      userInfo:dict];
}

最佳答案

谢谢 Chris,这看起来是问题所在,我已经根据此处的一些样板代码重写了代码: https://github.com/shrtlist/MCSessionP2P

现在,该应用程序无需浏览器即可自动连接到用户,这是一个很好的解决方案,我强烈建议任何有问题的人查看代码。

关于ios - Multipeer Connectivity 数据仅以一种方式发送,即使两个设备都已连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486224/

相关文章:

ios - swift:nscoding decodeObject 始终为 nil

ios - 如何将第二个 Controller 中的文本字段(嵌入在表格单元格中)中的字符串传递回第一个 Controller 中的标签(也嵌入在表格单元格中)?

ios - 导航到 Storyboard中的 View Controller 总是创建一个新的 View Controller ,我怎样才能重用以前的 View Controller ?

ios - 仅通过键盘在 IB 中添加约束

iphone - 更改 UINavigationController 推送动画的持续时间

objective-c - NS_ENUM 对象在添加到桥接头文件后无法编译

swift - 使用委托(delegate)方法在两个类之间发送数据

ios - 多点连接 - 使用 pin

ios - SFSafariViewController 标题

ios - TableView 单元格丢失顺序