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.
使用C ++编写的Cocos2dx为Android / Win32 / WP8创建游戏。 p>
我认为会起作用: p>
- 从Android / Win32上的游戏中使用cocos2dx网络库发送HTTP POST 使用来自游戏的数据向远程服务器请求。 li>
- 在服务器上安装MySQL数据库,使用PHP脚本从游戏中读取HTTP POST数据并将其存储在数据库中。 / li>
- 向网页显示信息 li>
ul>
我不需要从数据库中读取数据以将其发送回游戏 然而它只是发送和存储我感兴趣的数据,然后读取它显示为我可以连接的网页。 p>
我该怎么做? p >
谢谢。 p> div>
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 :)