Search code examples
iosnetworkingsendmultipeer-connectivity

Multipeer connectivity


I'm using this as a server-client peer-peer way, so that only the code on one device determines the outcome for the others.

So, I'm not very far into it.. But I'm setting up a lobby for connected players.

Client uses browser, then when a connection is established, the done button will push them to the LobbyViewController

Now, the match settings and details are set in the HostViewController then carried over to the lobbyViewController So they're set for the host

Then, if the client's say _matchName is equal to nil, then it sends a message to the host and the host can read this message. Then in return send back the _matchName.

However, I need to send a lot of messages throughout the rest of this application.. Send and receive I should say. And I don't want a whole lot of

if (_matchName == nil) {
    NSData *dataToSend = [@"getMatchName" dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers;
    NSError *error;

    [_appDelegate.mcManager.session sendData:dataToSend
                                     toPeers:allPeers
                                    withMode:MCSessionSendDataReliable
                                       error:&error];

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

-(void)didReceiveDataWithNotification:(NSNotification *)notification {
    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
    NSString *peerDisplayName = peerID.displayName;

    NSData *receivedData = [[notification userInfo] objectForKey:@"data"];
    NSString *receivedText = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSLog(@"From: %@ message: %@", peerDisplayName, receivedText);
}

For the first if, I don't want to have to go to that effort for a single message, nor do I want to create a response in the same form. Then a read the response message and update the _matchName property.. Seems like a whole lot of code, for a simple update of the matchName..

How, or is there a way to create a method to make this much simpler?


Solution

  • You'll likely need two forms of abstraction

    1. A singleton model class - call it MPManager or something to abstract the multipeer functionality - The singleton manages the send/receive browse/advertise parts of multipeer.
    2. A server-peer controller (again likely another singleton) that can manage the messages the server issues (from the view controller) and track responses from the individual peers back to the delegate who sent the message (viewController).

    You'd need these regardless of the underlying transport mechanism

    /*! 
    @class BWCMessageController
    @abstract
    The MessageController is a singleton object that is responsible for sending /
    receiving BWCMessage objects between devices.  It interfaces with the SessionController
    which is responsible for the actual sending/receiving, and acts as the delegate for
    received data.
    
    Once data is received, the controller reconstructs the BWCMessage and extracts the payload
    which is tehn forwarded to the handler object which is instantiated to further process the
    message
    */
    
    #import <Foundation/Foundation.h>
    #import "BWCMessage.h"
    #import "BWCSessionController.h"
    #import "BWCTransactionHandlerOrder.h"
    
    @interface BWCMessageController : NSObject <BWCSessionControllerDataDelegate>
    
    + (BWCMessageController *)messageController;
    
    - (BOOL)sendMsg:(MessageID)msgID withData:(NSData *)data fromHandler:(BWCTransactionHandler *)handler toDevice:(NSUInteger)devID withACK:(BOOL)fWithACK;
    
    
    @end