Multipeer Connectivity Framework - Lost Peer stays in Session

后端 未结 5 747
鱼传尺愫
鱼传尺愫 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;
        }
    }
    

提交回复
热议问题