iPhone客户端/服务器消息广播

时间:2010-12-27 21:19:16

标签: iphone client-server messaging gamekit

我一直在尝试使用5个iPod通过蓝牙创建一个简单的客户端/服务器网络。一个iPod将发送基本字符串,如“停止”或“转到”四个iPod,然后在收到命令时会执行各种操作。我目前正在使用GameKit 而没有 PeerPicker,即使启动连接也不可靠且复杂。连接后,我可以在两个设备之间发送数据,但不能更多。

我希望能够在没有网络(如遥控器)的情况下从iPod“服务器”向iPod“客户端”播放简单的短消息。我看过很多例子,包括坦克和wiTap的例子,但我很惊讶没有网络就没那么简单了。

以下是我开始连接的方式:

- (IBAction) btnConnect:(id)sender
{   
    if (sender == connectServer) {
        self.currentSession = [[GKSession alloc] initWithSessionID:@"0000"   
                                                           displayName:nil
                                                           sessionMode:GKSessionModeServer]; 
        NSLog(@"Setup Server Connection");

    } else { //connectClient
        //  Peers discover themselves ... 
        self.currentSession = [[GKSession alloc] initWithSessionID:@"0000"   
                                                       displayName:nil
                                                       sessionMode:GKSessionModeClient]; 
        NSLog(@"Setup Client Connection");
    }

    amAcceptingConnections = YES;
    [self.currentSession peersWithConnectionState:GKPeerStateAvailable];
    self.currentSession.delegate = self;
    self.currentSession.disconnectTimeout = 30;
    [self.currentSession setDataReceiveHandler:self  withContext:nil];

    //  Advertise the session to peers
    self.currentSession.available = YES;
}

我在委托

中处理与didChangeState的连接
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
    thePeerID = [session displayNameForPeer:peerID];
    NSLog(@"didChangeState was called from peerID: %@.", thePeerID);    
    self.currentSession = session;

    switch (state) {
        case GKPeerStateAvailable:
            thePeerID = [session displayNameForPeer:peerID];
            NSLog(@"Peer %@ Available", thePeerID);
            [session connectToPeer:peerID withTimeout:30];  
            NSLog(@"Issued Peer Connection");
            //session.available = NO;  //TODO: Look at this
            break;

        case GKPeerStateUnavailable:
            NSLog(@"Peer %@ Unavailable", thePeerID);
            break;

        case GKPeerStateConnected:
            NSLog(@"Peer %@ Connected", thePeerID);
            break;

        case GKPeerStateDisconnected:
            NSLog(@"Peer %@ Disconnected", thePeerID);
            [self.currentSession release];
            currentSession = nil;
            break;  
    }
}

麻烦的是didChangeState方法在好的和准备好时触发,并且行为是不可预测的。也许这是蓝牙或GameKit的问题,但我想在没有其他iPod“授权”连接的情况下进行连接。这可能吗?

认为我需要一台路由器和一个网络来实现这么简单的事情吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我做了很多研究,我决定使用一个小的5V无线接入点来建立我的多个iPod通信系统。 GameKit的东西很适合两个iPod互相交谈,但它不是很可靠(范围是另一个问题)。 CocoaAsyncSocket太棒了。似乎这里有很多人用它来进行TCP广播和通信。

以下是我采取的一些步骤:

  • 我将“控制器”iPod设置为打开与端口的连接。
  • 在我的“客户端”iPod应用中,我让他们使用静态IP连接到“控制器”作为主机
  • 然后我向客户端发送命令并根据命令执行各种操作

有没有人在本地网络中与多个iPod通信有什么成功?我很想知道你是如何完成这件事的。

快乐编码

相关问题