- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用主机/客户端方法来使用 MultiPeer 连接。
所以,当用户按下断开连接按钮时
-(IBAction)disconnect:(id)sender {
[_appDelegate.mcManager.session disconnect];
[_arrConnectedDevices removeAllObjects];
ConnectionsViewController *game = [self.storyboard instantiateViewControllerWithIdentifier:@"ConnectionsViewController"];
[self presentViewController:game animated:YES completion:nil];
}
现在,这工作正常。从主机的角度来看,它在日志中收到一条断开连接消息。并且客户端移动到新的 View Controller 。并且表格已更新。有了这个。
-(void)peerDidChangeStateWithNotification:(NSNotification *)notification{
MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
NSString *peerDisplayName = peerID.displayName;
MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];
if (state != MCSessionStateConnecting) {
if (state == MCSessionStateConnected) {
if (_makeSureImHost) {
[_arrConnectedDevices addObject:peerDisplayName];
[_tblConnectedDevices performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
else {
[self sendMessageToHostWithMessage:@"deviceInfo"];
}
}
else if (state == MCSessionStateNotConnected){
if ([_arrConnectedDevices count] > 0) {
int indexOfPeer = (int)[_arrConnectedDevices indexOfObject:peerDisplayName];
[_arrConnectedDevices removeObjectAtIndex:indexOfPeer];
NSLog(@"%@ Disconnected", peerDisplayName);
[_tblConnectedDevices performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
_tblConnectedDevices.frame = CGRectMake(_backgroundImage.frame.size.width / 2 - 150, self.backgroundImage.frame.size.height / 3, 300, 150);
}
}
}
}
大厅 View Controller 结束
开始连接 View Controller
当客户端按下浏览本地设备时运行
- (IBAction)browseForDevices:(id)sender {
[UIView animateWithDuration:0.5f
animations:^{
_searchButton.frame = CGRectMake(-100, self.backgroundImage.frame.size.height/2 + 60, 100, 35.0);
_hostButton.alpha = 0;
_modeLabel.alpha = 0;
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[_appDelegate mcManager] setupPeerAndSessionWithDisplayName:[UIDevice currentDevice].name];
[[_appDelegate mcManager] advertiseSelf:false];
[[_appDelegate mcManager] setupMCBrowser];
[[[_appDelegate mcManager] browser] setDelegate:self];
_appDelegate.mcManager.browser.maximumNumberOfPeers = 1;
_appDelegate.mcManager.browser.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentViewController:[[_appDelegate mcManager] browser] animated:YES completion:nil];
});
}
建立连接时
-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController {
[_appDelegate.mcManager.browser dismissViewControllerAnimated:NO completion:^{
[self launchViewController];
}];
}
-(void)launchViewController {
LobbyViewController *lobby = [self.storyboard instantiateViewControllerWithIdentifier:@"LobbyViewController"];
[self presentViewController:lobby animated:NO completion:nil];
}
由此
-(void)peerDidChangeStateWithNotification:(NSNotification *)notification {
MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
NSString *peerDisplayName = peerID.displayName;
MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];
if (state != MCSessionStateConnecting) {
if (state == MCSessionStateConnected) {
[self browserViewControllerDidFinish:[[_appDelegate mcManager] browser]];
}
}
else if (state == MCSessionStateNotConnected){
if ([_arrConnectedDevices count] > 0) {
int indexOfPeer = (int)[_arrConnectedDevices indexOfObject:peerDisplayName];
[_arrConnectedDevices removeObjectAtIndex:indexOfPeer];
}
}
}
现在。首次建立连接时。这一切都完美无缺。它连接,加载 View ,主机启动游戏,游戏运行正常,数据传输完美。
但是,如果您断开与大厅的连接。移动到连接 View Controller ,然后再次浏览设备。它将连接,但是大厅 View Controller 将不在 View 层次结构中,并将关闭浏览器并留在连接 View Controller 中。
然后,最重要的是,连接已经建立。然而,当它收到来自主机的消息时,它会发送响应,两次......或三次,或四次,将我引向死胡同。我唯一可以假设的是,从“客户”的角度来看,以某种方式记住了之前的 session 。
现在,我可以采取哪些步骤来避免这种困惑。如果我终止应用程序并重新启动它,我现在可以从客户端的角度再次连接。这让我相信,问题出在客户端。
我的问题是我必须彻底解决这个问题。因此断开连接将完全删除 session 中的所有内容。这样他们就可以重新连接了。并且不能依靠消息来告诉用户重新启动他们的应用程序。这是不可能的。
这是我的整个 MCManager.m 文件。
@implementation MCManager
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
return self;
}
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:_session];
}
-(void)advertiseSelf:(BOOL)shouldAdvertise{
if (shouldAdvertise) {
_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
discoveryInfo:nil
session:_session];
[_advertiser start];
}
else{
[_advertiser stop];
_advertiser = nil;
}
}
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
NSDictionary *dict = @{@"peerID": peerID,
@"state" : [NSNumber numberWithInt:state]
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification"
object:nil
userInfo:dict];
}
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
NSDictionary *dict = @{@"data": data,
@"peerID": peerID
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification"
object:nil
userInfo:dict];
}
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{
}
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
}
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID{
}
@end
#import <MultipeerConnectivity/MultipeerConnectivity.h>
@interface MCManager : NSObject <MCSessionDelegate>
@property (nonatomic, strong) MCPeerID *peerID;
@property (nonatomic, strong) MCSession *session;
@property (nonatomic, strong) MCBrowserViewController *browser;
@property (nonatomic, strong) MCAdvertiserAssistant *advertiser;
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName;
-(void)setupMCBrowser;
-(void)advertiseSelf:(BOOL)shouldAdvertise;
@end
如果有人知道我做错了什么,我将不胜感激。这让我抓狂。
最佳答案
[[NSNotificationCenter defaultCenter] removeObserver:name:object:];
解决了我所有的问题。希望也能帮助其他人。
关于ios - Multipeer Connectivity 终止 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112242/
有人知道 MCF 框架在不使用 MCBrowserViewController(将通过 MCNearbyServiceAdvertiserDelegate 和 MCNearbyServiceBrows
我的问题是关于多功能框的。 Chrome API 提供的想法,我想知道是否有可能在 list 上有多个关键字。 我在考虑正则表达式或类似的东西,但我真的不知道该怎么做...... 最佳答案 我想我刚刚
iOS12 Swift 5.x 我使用多点连接。到目前为止,如果我以正确的顺序连接我的设备,它运行良好。 使用样板代码,将问题追踪到此处。 func setupStream() { do {
当两台设备通过 Multipeer 连接连接时,我希望将两部手机中的任何一部调用给第三部手机。 这可能吗?对于前。 iphone1 和 iPhone 2 使用 multipeer 连接我想通过 iPh
我的设置是这样的:我同时运行 0 到 8 个设备,并且可以随时添加或删除设备。 我想使用 iOS 7 Multipeer 框架来连接它们。我让它在受控环境中工作,我可以在广告商模式下启动 1-7 台设
我正在努力解决一个问题。我想向连接的设备发送一个结构。 这是我使用的复制到 Playground 中的代码 在代码中,我为数据类添加了一些扩展用于编码/解码。我创建了 [String:Player]
嘿,我在 ios 中为聊天应用程序使用多对等连接框架。此框架是否支持任何加密。我想了解此框架中使用的加密,如 AES 或 RSA 加密系统?我的发现或研究: 1. https://datatheore
我想为我的应用程序使用 Multipeer Connectivity 功能。 APP功能简介:该应用程序应扫描运行该应用程序的其他设备(在后台),连接到它们并传输一些数据。所有这些都无需与用户交互。
目前我正在探索多点连接框架。我的应用程序 Advertiser & Browser 中有 2 个选项。 因此,当用户在一台设备上启动浏览器,而广告商在另一台设备上启动时,他们应该能够找到彼此。当设备出
我如何创建自定义邀请以显示来自广告商的发现信息? 这是来 self 的广告客户的代码: // create Discovery Info NSArray *objects=[[NSArray allo
我正在从教程网站学习 iOS 的多点连接框架 http://www.ralfebert.de/tutorials/ios-swift-multipeer-connectivity/ . 所提供的示例对
我正在使用 Multipeer Connectivity Framework在IOS7下做一个聊天应用。我正在使用内置的 MCBrowserViewController 来显示附近的同伴列表。 我想在
我有一个使用 MultiPeer Connectivity 框架的应用程序。每次应用程序在 AppDelegate 中变得活跃时,我都会创建一个新的 MCSession 一个 MCNearbyBrow
我在使用第三方库时遇到了问题,并且不确定解决该问题的常见模式是什么。 我正在使用 asi-http-request 类,它使用线程异步获取 http 对象。 在我的对象 dealloc() 方法中,我
我正在使用此方法要求附近的设备加入 session : 当我这样做时,我也开始旋转指标 [browser invitePeer:key toSession:session
我正在编写一个应用程序,它应该可以在没有连接到移动运营商且没有本地 WiFi 的情况下运行。每个设备都将充当发射器、接收器和路由器。到目前为止,我的主要挑战是我无法弄清楚 MultipeerConne
我正在使用主机/客户端方法来使用 MultiPeer 连接。 所以,当用户按下断开连接按钮时 -(IBAction)disconnect:(id)sender { [_appDelegate.
我有一个应用程序使用 Multipeer Connectivity 在设备之间发送文件。在接收设备上,didFinishReceivingResourceWithNamelocalURL 被调用,但是
我目前有两台设备通过多对等连接框架相互连接。连接后,每个设备都会立即相互发送一个 nsdata 数据包,在第一个实例中,数据将包含声音和图像。这似乎工作正常,并且两个用户都收到了相关数据。 用户现在可
我刚开始在 xcode 中使用 objective-c 编写 Multipeer 连接。我有几个关于基础知识的问题。 Multipeer Connectivity 的范围是多少?那么如果我做一个聊天程
我是一名优秀的程序员,十分优秀!