iPhone网络游戏

时间:2011-06-24 18:57:49

标签: iphone networking

我已经知道如何使用Wi-Fi和蓝牙制作在线游戏,但现在我正在尝试通过互联网实现一个简单的游戏(比如井字游戏),但我很丢失。

我尝试了GET / POST,但我不知道如何通知正在等待移动的玩家,以及如何保持游戏状态。

我是否必须在服务器端打开套接字,并从应用程序内部进行连接? 我已经进行了广泛的搜索,但我只能找到蓝牙和wifi,这不是我需要的。

谢谢大家!

2 个答案:

答案 0 :(得分:0)

去阅读Apple的WiTap Sample code,它应该与你想要做的相符。

另请咨询GameKit Programming Guide以了解如何让多个设备发现自己然后在游戏中(或其他任何东西)相互通信

答案 1 :(得分:0)

我会推荐这个库:http://code.google.com/p/cocoaasyncsocket/

对于服务器端,您可以使用node.js或Python Twisted之类的东西来打开套接字连接。

我自己的一个项目的示例代码。这将设置一个每10秒钟从主机读取的套接字。你的游戏会有所不同:

- (void) createSocket { 
  aisSocket = [[AsyncSocket alloc]initWithDelegate:self];
  NSError *error;
  [aisSocket connectToHost:myServerString 
                    onPort:myServerPort 
                     error:&error];
}


- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
  NSLog(@"socket will connect");
  return YES;
}


- (void) readData:(NSNotification*)note {
  [[note.userInfo objectForKey:@"sock"] readDataWithTimeout:100 tag:0];
}   


- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
  NSLog(@"The socket is connected: %d", [aisSocket isConnected]);
  NSDictionary *dict = [NSDictionary dictionaryWithObject:sock forKey:@"sock"];
  NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:(10)
                                                     target:self
                                                   selector:@selector(readData:)
                                                   userInfo:dict
                                                    repeats:YES]retain];
  [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
相关问题