Multipeer Connectivity Framework - Lost Peer stays in Session

后端 未结 5 587
鱼传尺愫
鱼传尺愫 2020-12-23 15:36

I wonder if this Multipeer Connectivity framework is ready for use in the real world, given all the bugs that have been encountered by the community. I think I\'m setting it

相关标签:
5条回答
  • 2020-12-23 16:01

    My only workaround to this type of issue has been to have a 1-1 relationship between sessions and peers. It complicates the sending of broadcasts, but at least allows for peer-level disconnects and cleanup through disconnecting/removing the session itself.

    Update

    To elaborate on my original answer, in order to be able to send data to connected peers it's necessary to maintain a reference to the session that was created for each peer. I've been using a mutable dictionary for this.

    Once the invitation has been sent/accepted with a new session, use the MCSession delegate method to update the dictionary:

    - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
    
        if (state==MCSessionStateConnected){
    
            _myPeerSessions[peerID.displayName] = session;
    
        }
        else if (state==MCSessionStateNotConnected){
    
            //This is where the session can be disconnected without
            //affecting other peers
            [session disconnect];            
    
            [_myPeerSessions removeObjectForKey:peerID.displayName];
        }
    }
    

    All peers can be accessed with a method that returns all values of the dictionary, and in turn all connectedPeers (in this case one) for each MCSession:

    - (NSArray *)allConnectedPeers {
    
       return [[_myPeerSessions allValues] valueForKey:@"connectedPeers"];
    
    }
    

    Sending data to a particular peer or via broadcast can be done with a method like this:

    - (void)sendData:(NSData *)data toPeerIDs:(NSArray *)remotePeers reliable:(BOOL)reliable error:(NSError *__autoreleasing *)error {
    
        MCSessionSendDataMode mode = (reliable) ? MCSessionSendDataReliable : MCSessionSendDataUnreliable;
    
        for (MCPeerID *peer in remotePeers){
    
           NSError __autoreleasing *currentError = nil;
    
           MCSession *session = _myPeerSessions[peer.displayName];
           [session sendData:data toPeers:session.connectedPeers withMode:mode error:currentError];
    
           if (currentError && !error)
            *error = *currentError;
        }
    }
    
    0 讨论(0)
  • 2020-12-23 16:06

    I've been having similar problems. It seems though that if I have run my app on one iOS device, and connected to another, then quit and relaunch (say when I rerun from Xcode), then I am in a situation where I get a Connected message and then a Not Connected message a little later. This was throwing me off. But looking more carefully, I can see that the Not Connected message is actually meant for a different peerId than the one that has connected.

    I think the problem here is that most samples I've seen just care about the displayName of the peerID, and neglect the fact that you can get multiple peerIDs for the same device/displayName.

    I am now checking the displayName first and then verifying that the peerID is the same, by doing a compare of the pointers.

    - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
    
        MyPlayer *player = _players[peerID.displayName];
    
        if ((state == MCSessionStateNotConnected) &&
            (peerID != player.peerID)) {
            NSLog(@"remnant connection drop");
            return; // note that I don't care if player is nil, since I don't want to
                    // add a dictionary object for a Not Connecting peer.
        }
        if (player == nil) {
            player = [MyPlayer init];
            player.peerID = peerID;
            _players[peerID.displayName] = player;
        }
        player.state = state;
    
    ...
    
    0 讨论(0)
  • 2020-12-23 16:09

    Have you tried disconnecting the session before the application closes? This should remove the peer from the session properly and cleanup any resources allocated for the peer.

    Specifically I mean something like [self.peer disconnect] in applicationWillTerminate:

    0 讨论(0)
  • 2020-12-23 16:10

    I couldn't get the accepted answer to ever work, so what i did instead is have a timer that would fire to reset the connection when the browser would report not connected and there were no other connected peers.

    -(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
    
    //DebugLog(@"session didChangeState: %ld",state);
    
    if(resetTimer != nil){
        [resetTimer invalidate];
        resetTimer = nil;
    }
    
    if(state == MCSessionStateNotConnected){
    
        [session disconnect];
        [peerSessions removeObjectForKey:peerID.displayName];
        [self removeGuidyPeerWithPeerID:peerID];
        //DebugLog(@"removing all guides from peer %@",peerID);
    
        if([localSession connectedPeers].count == 0){
    
            DebugLog(@"nothing found... maybe restart in 3 seconds");
            dispatch_async(dispatch_get_main_queue(), ^{
                resetTimer = [NSTimer
                          scheduledTimerWithTimeInterval:3.0
                          target:self selector:@selector(onResetTimer:)
                          userInfo:nil
                          repeats:NO];
                }
            );
        }
    }
    ...
    

    }

    0 讨论(0)
  • 2020-12-23 16:10

    You can delete the peer from the MCBrowserViewController with following code in Swift 3:

    self.mySession.cancelConnectPeer(self.myPeerID)
    
    0 讨论(0)
提交回复
热议问题