2011-06-24 199 views
0

我已经知道如何使用Wi-Fi和蓝牙在线游戏,但现在我试图通过互联网实现一个简单的游戏(如井字游戏),但我是亲切的失落。iPhone网络游戏

我试过GET/POST,但我不知道如何通知一个等待移动的玩家,以及如何保存游戏状态。

我必须在服务器端打开一个套接字,并从应用程序内连接吗? 我已经做了大量的搜索,但我只能找到关于蓝牙和wifi,这不是我需要的。

谢谢大家!

回答

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]; 
} 
+0

谢谢你们的答案。我已经下载了asynchSock,现在我可以连接服务器。我有一个php脚本为我创建服务器套接字。我现在怀疑的是如何设置游戏结构...我如何同步用户/游戏?再次感谢! – Fernando

+0

我认为本教程将帮助你:http://www.raywenderlich.com/3276/how-to-make-a-simple-multiplayer-game-with-game-center-tutorial-part-12 –

+0

你也可能会想要在这个论坛上列出的书 - http://www.cocos2d-iphone.org/forum/topic/12893 –

0

去看苹果的WiTap Sample code,它应该匹配你正在尝试做的事情。

亦可以参考GameKit Programming Guide知道如何使多个设备发现自己然后彼此在游戏(或其他任何东西)通信

+0

我已经知道如何做无线网络和bounjour的东西。问题是与服务器相关的使用哪些工具...另外,我不知道是否必须将游戏逻辑放在服务器上,或者只使用服务器将数据包发送到其他连接的设备。 – Fernando