我将其用作服务器-客户端对等方式,因此只有一台设备上的代码决定其他设备的结果。
所以,我并没有很深入。但我正在为连接的玩家建立一个大厅。
客户端使用浏览器,当建立连接时,完成按钮将它们推送到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)didReceiveDataWithNotificationNSNotification *)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..
如何,或者有没有办法创建一种方法来使这变得更简单?
您可能需要两种形式的抽象
无论底层传输机制如何,您都需要这些
/*!
@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)sendMsgMessageID)msgID withDataNSData *)data fromHandlerBWCTransactionHandler *)handler toDeviceNSUInteger)devID withACKBOOL)fWithACK;
@end
关于ios - 多点连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28724557/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |