• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

HTML5 Setting up http_get and http_post_string to create an online game

Coded Games

Member
Hey guys. So I'm working on a HTML5 card game and a lot of people requested that I try to add an online mode to the game. Since it's just a card game I don't need the online to run fast so I thought that the http_* functions might work.

Unfortunately this is the first time I've ever used these functions and I'm not entirely sure what I need to setup to get them working. All I really need to do is have to a client send a string representing the card they would to play to another client and repeat that back and forth a few times.

I see how http_get works but I'm a bit confused with http_post_string, do I need to do something with my website to get it working? If someone has any experience using these features in their game any advice would be greatly appreciated.
 
I

icuurd12b42

Guest
See http_post_string

The example should be enough to read the content for (any) the site you http to

the result key in the ds map of the async event holds the web page content.

You need active server page ability on your web server; server side scripting like php or vbscript or javascript.

With server side scripting you can request.write the data you whish the game to read in it's http post. it can be plain text... or a json text which is easily convertible on the game side. you can pass data between 2 players but you need to setup a database for storing the data you wish to pass back and forth and to link players. It maight be possible to use the server app instance memory for this too but a database would support more players.

But first thing first. can you do http from an html5 game?
 

FrostyCat

Redemption Seeker
Of course it's possible to send HTTP requests from an HTML5 game, though you have to handle CORS and preflighting if the server-side scripts aren't served off the same origin (i.e. same domain, same port).

Just having a URL to POST to isn't enough, you need server-side scripting to act upon the request. I have a whole bunch of examples on this page for basic framework-free PHP. Anything capable of handling HTTP traffic and acting upon it will do --- I've done PHP, Rails and Node.js.

A faster strategy than HTTP is WebSockets, which you can tap into with this extension and handle from the server side in Node.js. Then you can use MongoDB for any persistent data such as players and statistics, and Redis for non-persistent data such as game sessions.
 
Top