douhe6181 2015-02-20 08:48
浏览 112
已采纳

Cocos2dx C ++将游戏数据发送到远程服务器上的MySQL数据库

Creating a game for Android/Win32/WP8 using Cocos2dx written in C++.

I want to send basic game statistics to a remote MySQL database so they can be viewed on a webpage.

What I think will work:

  • From the game on Android/Win32 use the cocos2dx network libraries to send a HTTP POST request to the remote server with the data from the game.
  • On the server have MySQL database setup, use a PHP script to read the HTTP POST data from the game and store it in the database.
  • Display the information to a webpage

I don't need to read data from the database to send it back to the game just yet so its just sending and storing the data that I'm interested in, then reading it to be displayed as a webpage I can connect to.

How do I do this?

Thanks.

  • 写回答

1条回答 默认 最新

  • douxian0008 2015-04-27 09:59
    关注

    I figured it out. For anyone else looking to do the same thing.

    • I installed XAMPP to set up a server that runs locally at http://localhost.
    • I then created a PHP script that read a $_POST['username'] array variable to extract all of the key/value pairs from the HTTP POST from the game.
    • From the Cocos2dx game I used the HTTPClient library to created a HTTP Request
    • The Request data contained a string with a key/value pair so that the PHP script could read it on the server.
    • The string looked like this:

      'username=damorton&score=playerScore'
      
    • Send the request from the game using request->send(data); and specify http://localhost as the target.

    • The PHP script can then identify the keys username and score by referencing its name in the array variable :

      $name = $_POST['username']
      $score = $_POST['score']
      

    From here I just connected to a local MySQL database that I set up in XAMPP. Create some SQL queries in the PHP scripts for reading and writing the values stored in $name and $score. All communication with the database happens in the PHP scripts. The game interfaces with the PHP on the server to read and write to the database. And that was it.

    I have since moved the local server to a remote one by copying the PHP scripts and using the domain name to access the PHP scripts.

        http://www.insertyourdomainnamehere.com/update_database.php
    

    The game now uses an online leaderboard, is available on the Windows Store, and will update all of the players stats remotely.

    Hope this helps someone out :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大