gpt4 book ai didi

ios - MCNearbyServiceBrowser 如何知道 MCNearbyServiceAdvertiser 是否拒绝了邀请?

转载 作者:可可西里 更新时间:2023-11-01 03:54:12 24 4
gpt4 key购买 nike

iOS Multipeer 连接问题...

如果 MCNearbyServiceAdvertiser 通过调用拒绝来自 MCNearbyServiceBrowser 的邀请:

invitationHandler(NO, nil);

...在:

广告商:didReceiveInvitationFromPeer:withContext:invitationHandler:

...有没有办法让 MCNearbyServiceBrowser 知道邀请被明确拒绝

我确实看到当广告商拒绝邀请时,使用 MCNearbyServiceBrowser 的设备收到 session 状态更改为 MCSessionStateNotConnected,但我想浏览设备可能会收到 MCSessionStateNotConnected 也出于其他原因...例如广告商设备消失(关闭等)。

关于如何区分拒绝邀请和其他类型的断开连接有什么建议吗?

谢谢。

-艾伦

最佳答案

根据文档,MCSessionStateNotConnected 可以表示

"...the nearby peer declined the invitation, the connection could not be established, or a previously connected peer is no longer connected."

你没有在你的问题中说什么会导致邀请被拒绝,但假设它是用户驱动的,一种方法可能是让你的 MCNearbyServiceAdvertiserDelegate 自动接受邀请,创建一个新的每个对等点的 session ,然后向用户提供接受或拒绝连接的选择。在收到指示用户决定的后续消息之前,您的同龄人不会被视为真正处于 session 中:

因此,在您的 MCNearbyServiceAdvertiserDelegate 类中,您将接受并提示用户:

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID
withContext:(NSData *)context
invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler {

//Automatically accept with a new session
MCSession *newSession = [[MCSession alloc] initWithPeer:_myPeerID];
newSession.delegate = self;

//Keep track of the pending sessions in a mutable dictionary
_pendingSessionPeerIDMap[peerID.displayName] = newSession;

invitationHandler(YES,newSession);

/* Code here to present user with option to accept or decline peer */
}

然后当用户响应时,你可以有一个方法发送一个简单的字符串作为包含状态的 NSData:

@property NSData *inviteAcceptedMsgData = [@"MPCInviteYES" dataUsingEncoding:NSUTF8StringEncoding];
@property NSData *inviteDeclinedMsgData = [@"MPCInviteNO" dataUsingEncoding:NSUTF8StringEncoding];

- (void)invitationFromPeerID:(MCPeerID *)peerID receivedResponse:(BOOL)accepted {

//Send a message to the peer that sent the invitation indicating whether the
//user accepted or declined

NSData *msgData = (accepted) ? _inviteAcceptedMsgData : _inviteDeclinedMsgData;

MCSession *peerSession = _pendingSessionPeerIDMap[peerID.displayName];

[peerSession sendData:msgData
toPeers:@[peerID]
withMode:MCSessionSendDataReliable
error:nil];

//Remove the pending session
[_pendingSessionPeerIDMap removeObjectForKey:peerID.displayName];

//And if the connection was accepted by the user, add to an accepted dictionary
_acceptedSessionPeerIDMap[peerID.displayName] = peerSession;
}

MCNearbyServiceBrowserDelegate 的工作方式类似:

- (void)browser:(MCNearbyServiceBrowser *)browser 
foundPeer:(MCPeerID *)peerID
withDiscoveryInfo:(NSDictionary *)info {

//Send the invitation with a new session
MCSession *newSession = [[MCSession alloc] initWithPeer:_myPeerID];
newSession.delegate = self;

[browser invitePeer:peerID
toSession:newSession
withContext:nil
timeOut:0];
}

然后浏览器将等待来自它邀请的对等点的消息以确定邀请是否真正被接受:

- (void)session:(MCSession *)session 
didReceiveData:(NSData *)data
fromPeer:(MCPeerID *)peerID {

//Check the data to see whether invite was accepted
if ([data isEqualToData:_inviteAcceptedMsgData]) {

//Add to an accepted dictionary
_acceptedSessionPeerIDMap[peerID.displayName] = session;
}
else if ([data isEqualToData:_inviteAcceptedMsgData]){

//Disconnect the session
[session disconnect];
}
}

您可能已经有一个消息传递系统,但这代表了在连接的对等点之间传递状态的简单实现。就发送广播或显示连接的点而言,_acceptedSessionPeerIDMap 应该用作点的真实集合。

关于ios - MCNearbyServiceBrowser 如何知道 MCNearbyServiceAdvertiser 是否拒绝了邀请?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com