Iphone 使用GameKit连接第三台设备
我现在正在基本上使用GameKit。我能够连接两台设备并在它们之间发送消息 我有3个设备,我们称它们为设备A、B和C 我能够将A连接到B、A连接到C、B连接到C,作为单独的设置 如果我将A连接到B,然后尝试将B连接到C,设备C将显示设备B可用,但设备B继续旋转并说“正在寻找可用的iPod、iPhone…” 在Iphone 使用GameKit连接第三台设备,iphone,gamekit,gksession,Iphone,Gamekit,Gksession,我现在正在基本上使用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。。。因为
peerPickerController:sessionForConnectionType:
中,当我试图将B连接到C时,我试图让设备B重用它在连接到A时使用的相同GKSession
。。。因为如果我在设备B上创建新会话,它可以连接到设备C,但会断开与设备a的连接
以下是连接类型的会话:
-(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;
}
我最终采用了服务器/客户端设置,这更易于管理。这样,只有一个服务器PeerID,而不是每个连接都有一个服务器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
我最终采用了服务器/客户端设置,这更易于管理。这样,只有一个服务器PeerID,而不是每个连接都有一个服务器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
嗨,克里斯,我想为iOS设备实现客户机-服务器模型。我找不到太多关于这方面的信息,你能给我推荐一些我可以使用的指针或资源吗?@SumitLonkar这是我写的一篇关于这个的帖子()嘿,Chris,我想为iOS设备实现客户机-服务器模型。我找不到太多关于这方面的信息,你能给我推荐一些我可以使用的指针或资源吗?@SumitLonkar这是我写的一篇关于这个()