【问题标题】:iPhone Client/Server Message BroadcastingiPhone 客户端/服务器消息广播
【发布时间】:2010-12-27 21:19:16
【问题描述】:

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

我希望能够从没有网络的 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“授权”连接的情况下进行连接。这可能吗?

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

谢谢

【问题讨论】:

    标签: iphone client-server messaging gamekit


    【解决方案1】:

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

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

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

    有没有人成功地与本地网络中的多个 iPod 通信?我很想看看你是如何做到这一点的。

    愉快的编码

    【讨论】:

      猜你喜欢
      • 2016-02-24
      • 2018-11-14
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多