gpt4 book ai didi

iphone - GameKit:GKSession 手册

转载 作者:行者123 更新时间:2023-12-03 18:48:11 28 4
gpt4 key购买 nike

我想使用 GKSession 连接两台设备,其中一台作为服务器,另一台作为客户端。使用此配置我无法使用 GKPeerPickerController。

我在连接两个设备时遇到问题:

  • 仅使用蓝牙:不可能
  • 使用WiFi:设备之间至少有一些数据交换,但没有成功连接。

在接口(interface)文件中我有

GKSessionDelegate
GKSession *session;

在实现中,我使用以下代码启动服务器:

session = [[GKSession alloc] initWithSessionID:@"iFood"  displayName:nil sessionMode:GKSessionModeClient];
session.delegate = self;
session.available = YES;

客户端开始使用此代码:

session = [[GKSession alloc] initWithSessionID:@"iFood"  displayName:nil sessionMode:GKSessionModeServer];
session.delegate = self;
session.available = YES;

如何强制使用蓝牙而不是 WiFi?

我还实现了这些调用:

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
NSLog(@"Someone is trying to connect");
}

- (BOOL)acceptConnectionFromPeer:(NSString *)peerID error:(NSError **)error {
NSLog(@"acceptConnectionFromPeer");
}

当我开始时,我将其输入调试器:

Listening on port 50775
2010-02-19 14:55:02.547 iFood[3009:5103] handleEvents started (2)

当其他设备开始查找时,我得到以下信息:

~ DNSServiceBrowse callback: Ref=187f70, Flags=2, IFIndex=2 (name=[en0]), ErrorType=0 name=00eGs1R1A..Only by Audi regtype=_2c3mugr67ej6j7._udp. domain=local.
~ DNSServiceQueryRecord callback: Ref=17bd40, Flags=2, IFIndex=2 (name=[en0]), ErrorType=0 fullname=00eGs1R1A\.\.Only\032by\032Audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=4500
** peer 1527211048: oldbusy=0, newbusy=0
~ DNSServiceBrowse callback: Ref=187f70, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 name=00eGs1R1A..Only by Audi regtype=_2c3mugr67ej6j7._udp. domain=local.
GKPeer[186960] 1527211048 service count old=1 new=2
~ DNSServiceQueryRecord callback: Ref=17bd40, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 fullname=00egs1r1a\.\.only\032by\032audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=7200
** peer 1527211048: oldbusy=0, newbusy=0
~ DNSServiceBrowse callback: Ref=187f70, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 name=00TF5kc1A..Only by Audi regtype=_2c3mugr67ej6j7._udp. domain=local.
~ DNSServiceQueryRecord callback: Ref=188320, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 fullname=00tf5kc1a\.\.only\032by\032audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=7200
** peer 1723356125: oldbusy=0, newbusy=0
~ DNSServiceQueryRecord callback: Ref=188320, Flags=2, IFIndex=2 (name=[en0]), ErrorType=0 fullname=00TF5kc1A\.\.Only\032by\032Audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=4500
** peer 1723356125: oldbusy=0, newbusy=0

我在这里缺少什么?

我确信两台设备都启用了蓝牙并连接到同一个 WiFi。

谢谢

r。

最佳答案

我认为您错过了接受与客户端的连接。收到“didReceiveConnectionRequestFromPeer”回调后,您需要接受与客户端的连接,如下所示:

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
NSLog(@"Someone is trying to connect");
NSError *error;
[gkSession acceptConnectionFromPeer:peerID error:&error];
if(error)
NSLog(@"Error on accept connection with peer: ", error);
}

此后,您将在此处收到“GKPeerStateConnected”:

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{}

希望这对您有帮助。

关于iphone - GameKit:GKSession 手册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2296856/

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