【问题标题】:Multipeer connectivity多点连接
【发布时间】:2015-02-25 16:38:12
【问题描述】:

我将其用作服务器-客户端对等方式,因此只有一台设备上的代码决定其他设备的结果。

所以,我并没有很深入。但我正在为连接的玩家建立一个大厅。

客户端使用浏览器,当建立连接时,完成按钮将他们推送到LobbyViewController

现在,匹配设置和详细信息在HostViewController 中设置,然后转移到lobbyViewController 所以它们是为host 设置的

然后,如果客户端说_matchName 等于nil,那么它会向host 发送消息,host 可以读取此消息。然后作为回报发回_matchName

但是,我需要在此应用程序的其余部分发送大量消息。我应该说发送和接收。而且我不想要很多

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);
}

对于第一个 if,我不想为了一条消息而费尽心思,也不想以相同的形式创建响应。然后阅读响应消息并更新_matchName 属性.. 似乎有很多代码,用于简单更新 matchName..

如何,或者有没有办法创建一种方法来使这变得更简单?

【问题讨论】:

    标签: ios networking send multipeer-connectivity


    【解决方案1】:

    您可能需要两种形式的抽象

    1. 单例模型类 - 将其称为 MPManager 或其他名称 抽象多点功能 - 单例管理 发送/接收浏览/广告多人的部分。
    2. 一个服务器对等控制器(也可能是另一个单例),它可以 管理服务器发出的消息(来自视图控制器)并跟踪来自各个对等方的响应返回给发送消息的委托(视图控制器)。

    无论底层传输机制如何,您都需要这些

    /*! 
    @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
    

    【讨论】:

    • 有什么方法可以详细说明来达到这样的结果?
    • 首先,做一些谷歌搜索 - 以确保您了解单例。有一点涉及,需要对这个概念有很好的理解。我在上面的一个课程中添加了一个示例
    • 非常感谢。我感谢额外的帮助。看来我需要更多学习!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多