使用 GameKit 连接第三台设备

发布于 2024-10-29 13:43:27 字数 869 浏览 1 评论 0原文

我现在正在基础水平上使用 GameKit。我能够连接两个设备并在它们之间发送消息。

我有 3 个设备,我们将它们称为设备 A、B 和 C。

我能够将 A 连接到 B、A 连接到 C、B 连接到 C,作为单独的设置。

如果我将 A 连接到 B,然后尝试将 B 连接到 C,设备 C 将显示设备 B 可用,但设备 B 继续旋转并说“正在寻找可用的 iPod、iPhone...”

在peerPickerController 中: sessionForConnectionType: ,当我尝试将 B 连接到 C 时,我试图让设备 B 重用它在连接到 A 时使用的相同 GKSession...因为如果我在设备 B 上创建一个新会话,它能够连接到设备 C,但会断开与设备 A 的连接。

以下是 sessionForConnectionType

 -(GKSession*)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {

   // session is a synthesized GKSession
        if (session == nil) {
            session = [[GKSession alloc] initWithSessionID:nil  displayName:@"" sessionMode:GKSessionModePeer];
            session.delegate = self;        
        } 


        return session;
    }

I am using GameKit on a basic level right now. I am able to connect two devices and send messages between them.

I have 3 devices, we'll call them device A, B and C.

I am able to connect A to B, A to C, and B to C, as individual setups.

If I connect A to B, then attempt to connect B to C, Device C will show that Device B is available, but device B continues to spin and say "Looking for available iPod, iPhones..."

In peerPickerController:sessionForConnectionType: , when I am attempting to connect B to C, I am trying to have device B reuse its same GKSession that it is using in its connection to A... because if I create a new session on device B, it is able to connect to Device C but drops the connection to device A.

Here is the sessionForConnectionType :

 -(GKSession*)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {

   // session is a synthesized GKSession
        if (session == nil) {
            session = [[GKSession alloc] initWithSessionID:nil  displayName:@"" sessionMode:GKSessionModePeer];
            session.delegate = self;        
        } 


        return session;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-11-05 13:43:27

我最终选择了服务器/客户端设置,这更易于管理。这样,就只有一个 Server PeerID,而不是每个连接都有一个 Server PeerID。我找不到很多好的例子,所以我在这里包含了基本的 GameKit 服务器/客户端代码。

// if the device in an ipad, treat it as a host / server
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]) {
        isHost = YES;
    } else {
        isHost = NO;
    }

// monitor if this device is connected to another device
    isConnected = NO;
}




#pragma mark GameKit Methods

// Called when a change in the connection state is detected
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {

    NSLog(@"Session:Peer:%@ Did Change State", peerID);
     Globals *globals = [Globals shareData];

    switch (state) {
        case GKPeerStateConnected:
            NSLog(@"GKPeerStateConnected");

            [globals.localSession setDataReceiveHandler:self withContext:nil];

// if this device is not the host and is not connected yet...
            if (!isHost && !isConnected) {

// update variables, save the Server PeerId and the local Session so we can use them later 
                isConnected = YES;
                serverSession = peerID];
                localSession = session;
            }
            break;

        case GKPeerStateDisconnected:
            NSLog(@"GKPeerStateDisconnected");
            break;

        case GKPeerStateAvailable:
            NSLog(@"GKPeerStateAvailable");
            if (!isHost) {
                NSLog(@"Attempting to Connect...");
// the server is available, try to connect to it
                [session connectToPeer:peerID withTimeout:20];
            }
            break;

        case GKPeerStateConnecting:
            NSLog(@"GKPeerStateConnecting");
            break;

        case GKPeerStateUnavailable:
            NSLog(@"GKPeerStateUnavailable");
            break;
    }

}

// Called if this device receives a request for a connection
// This should only happen on the Server device
-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
    NSLog(@"Received Connection Request From %@", peerID);

// Accept the connection request from the peer
    [session acceptConnectionFromPeer:peerID error:nil];
}

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {
    NSLog(@"Received Data");   
}

-(void)session:(GKSession *)session didFailWithError:(NSError *)error {
    NSLog(@"Session Failed: %@", error);
}


// Connected to a UIButton
-(IBAction)sendData {
    NSLog(@"Sending Data ...");   
}

// Connected to a UIButton
-(IBAction)beginConnection {

    Globals *globals = [Globals shareData];

// Set this up as a server
    if (isHost) {
        GKSession *session = [[GKSession alloc] initWithSessionID:@"" displayName:@"Server" sessionMode:GKSessionModeServer];
        session.delegate = self;
        session.available = YES;       
        NSLog(@"Setting Server Session Peer:%@",  session.peerID);
        globals.localSession = session;
    } 

// or set it up as a client
else {
        GKSession *session = [[GKSession alloc] initWithSessionID:@"" displayName:nil sessionMode:GKSessionModeClient];
        session.delegate = self;
        session.available = YES;
        NSLog(@"Setting CLIENT Session Peer:%@", session.peerID);
        globals.localSession = session;
    }

}


... Dealloc, etc...

@end

I ended up going with a Server / Client setup, which is easier to manage. This way, there is only one Server PeerID, as opposed to a Server PeerID for every connection. I wasn't able to find many good examples, so I've included the basic GameKit Server / Client code here.

// if the device in an ipad, treat it as a host / server
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]) {
        isHost = YES;
    } else {
        isHost = NO;
    }

// monitor if this device is connected to another device
    isConnected = NO;
}




#pragma mark GameKit Methods

// Called when a change in the connection state is detected
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {

    NSLog(@"Session:Peer:%@ Did Change State", peerID);
     Globals *globals = [Globals shareData];

    switch (state) {
        case GKPeerStateConnected:
            NSLog(@"GKPeerStateConnected");

            [globals.localSession setDataReceiveHandler:self withContext:nil];

// if this device is not the host and is not connected yet...
            if (!isHost && !isConnected) {

// update variables, save the Server PeerId and the local Session so we can use them later 
                isConnected = YES;
                serverSession = peerID];
                localSession = session;
            }
            break;

        case GKPeerStateDisconnected:
            NSLog(@"GKPeerStateDisconnected");
            break;

        case GKPeerStateAvailable:
            NSLog(@"GKPeerStateAvailable");
            if (!isHost) {
                NSLog(@"Attempting to Connect...");
// the server is available, try to connect to it
                [session connectToPeer:peerID withTimeout:20];
            }
            break;

        case GKPeerStateConnecting:
            NSLog(@"GKPeerStateConnecting");
            break;

        case GKPeerStateUnavailable:
            NSLog(@"GKPeerStateUnavailable");
            break;
    }

}

// Called if this device receives a request for a connection
// This should only happen on the Server device
-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
    NSLog(@"Received Connection Request From %@", peerID);

// Accept the connection request from the peer
    [session acceptConnectionFromPeer:peerID error:nil];
}

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {
    NSLog(@"Received Data");   
}

-(void)session:(GKSession *)session didFailWithError:(NSError *)error {
    NSLog(@"Session Failed: %@", error);
}


// Connected to a UIButton
-(IBAction)sendData {
    NSLog(@"Sending Data ...");   
}

// Connected to a UIButton
-(IBAction)beginConnection {

    Globals *globals = [Globals shareData];

// Set this up as a server
    if (isHost) {
        GKSession *session = [[GKSession alloc] initWithSessionID:@"" displayName:@"Server" sessionMode:GKSessionModeServer];
        session.delegate = self;
        session.available = YES;       
        NSLog(@"Setting Server Session Peer:%@",  session.peerID);
        globals.localSession = session;
    } 

// or set it up as a client
else {
        GKSession *session = [[GKSession alloc] initWithSessionID:@"" displayName:nil sessionMode:GKSessionModeClient];
        session.delegate = self;
        session.available = YES;
        NSLog(@"Setting CLIENT Session Peer:%@", session.peerID);
        globals.localSession = session;
    }

}


... Dealloc, etc...

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文