我将此作为服务器 - 客户端对等方式使用,以便只有一台设备上的代码才能确定其他设备的结果。
所以,我并不是很远......但是我为连接的玩家设置了一个大厅。
客户端使用浏览器,然后在建立连接时,完成按钮会将它们推送到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的简单更新..
如何,或者有没有办法创建一种方法来使这更简单?
答案 0 :(得分:1)
您可能需要两种抽象形式
无论基础传输机制如何,您都需要这些
/*!
@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