how's the best way to do it JS auto load..

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: how's the best way to do it JS auto load..

Post by Chris »

This is easily possible with an reverse-ajax/long-polling request or by using the new HTML5 standard for websocket.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: how's the best way to do it JS auto load..

Post by Jackolantern »

Chris wrote:This is easily possible with an reverse-ajax/long-polling request or by using the new HTML5 standard for websocket.
"Easily" is quite a subjective word there ;) The issue with both Comet and Websockets is that you are basically leaving the world of traditional web development and entering real client/server architecture. You will need to actually be running an application server with a Comet or Websocket server to receive and respond to the requests. The server would look more like a regular MMORPG server than a PBBG, and that adds craploads of complexity, even using something like node.js, which eases off a lot of the complexity of making a server in Java or C#.

My two cents is that the important thing you said, Oroton, is that it is turn-based. With such low numbers of players, you could just use traditional Ajax polling that will check on an interval if there is new data. Of course, this won't scale well if your game does get popular, and it will essentially break down and hit your brandwidth cap by around 200 players logged in at once. Ajax polling wastes tons of bandwidth.

Of course, don't let me talk you out of going Comet or Websockets (or, as most modern frameworks are using, Websockets first and then falling back on Flash and then Comet). Just understand that it will be quite a long-haul to get to where you are developing your game at ease with these newer, more complex technologies due to the requirements they add.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: how's the best way to do it JS auto load..

Post by Chris »

My advice would be to use comet. It is very easy to achieve with a standard Apache/PHP server. To make it work dynamically between players will require some thought however to design the system. You are going to connect the players together, something which is much easier achieved with a single thread process like Node.js. That's when I start thinking in the direction of sockets, but you could store battle logs/lobbies in a database and poll them for an update server-side.

Here's the idea:

Code: Select all

<?php
include 'connect.php';

// database poll
$query = mysql_query("SELECT * FROM `table` WHERE `last_timestamp` > '". (int) $_GET['time'] ."';");
while( mysql_num_rows($query) == 0 ) // keep looping as long as there are no results
{
    set_time_limit(5); // prevent the connection from closing
    usleep(500000); // wait 0.5 seconds
    $query = mysql_query("SELECT * FROM `table` WHERE `last_timestamp` > '". (int) $_GET['time'] ."';");
}

// loop has finished, generate result
$result = array();
while( $row = mysql_fetch_assoc($result) )
{
    $result[] = $row;
}

echo json_encode($result); // script has finished, send data back in JSON format
 
If you are working with sessions another function you'll need is session_write_close. Only one script can write to the session at once, other scripts running in the background have to wait until writing is complete/disabled.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: how's the best way to do it JS auto load..

Post by Chris »

Until that time I wouldn't worry about it. There are two main problems with this. Database scalability and bandwidth. Keeping the database under control is easy as you could simply delete records as they are not needed anymore. Bandwidth is something I personally don't worry about too much. If you are using all your bandwidth, that means your game would only be doing well. I think the internet is always needing upgrades and there are people who need to put pressure on it. If you require more bandwidth, make the net come with the tech to achieve it.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: how's the best way to do it JS auto load..

Post by Jackolantern »

Wait...that is Comet? That doesn't look too complicated! Why are all of these sites talking about Comet servers? It seems like this way could be rather CPU intensive (but as you mentioned, probably not worth it to worry too much about that, since having thousands of online players is really just a dream). Since Comet is just an umbrella term for several different techniques to push data, are there other, less CPU intensive methods that do require a server application?
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: how's the best way to do it JS auto load..

Post by Chris »

In Node.js this is very easily achieved as you can store buffers in the same thread as the event based triggering system works on, the know-how that is then required is deciding when to add and remove data from the buffers, these buffers can be modeled using classes with JavaScript. JavaScript's object oriented style programming is the only drawback here as it can quickly eat memory. This method offers quite a difference in performance when compared to sending constant queries to a database server. If you know how to limit the amount of data that has to be processed in one thread. You could easily build your game physics and commit loops straight into a game server. It then all boils down to knowing when to properly commit data to your central database.

From what I understood before I started using Node.JS, PHP offers the same sort of thing with its socket library, HTTP 1.0 does not allow for websockets, therefor never really required the need for keeping connections open for a long time. PHP was designed to produce data as quickly as possible, so it could be sent to a client through a webserver. This is quite a bit easier to develop into a system that can run on multiple threads as the webserver can act as the bootstrap that runs a new PHP instance each time a request is made. PHP can store these instances in a buffer, the only problem with PHP is, to get information from this buffer, you need to create a socket to the thread on which the buffer is found, before you are able to read or manipulate it. There are probably frameworks or servers built to make these steps easier for PHP to achieve, they however most likely do not offer any magical performance upgrades when it comes down to polling from the client. One method I developed once was to hold the connection open as long as possible and simply poll a MySQL database that acted as my buffer, it scaled fine and was not of any concern, the only stumble I had was sending requests from the client and identifying the user with their session, the sessions are hard to manipulate as only one process can have access to the session buffer at once. I later went on to develop a system that would allow the client keep multiple connections open with a server, then held information which process would return next, I stopped this project when Node.JS came out and might still have the source. I think this technique is also used by socket.io when it resorts to reverse-ajax, only without the need of polling a database of course, the option is however always there.

My first assumption when doing this would be to make a trigger system in MySQL that would run a process to allow PHP to return to the client, this was all completely unnecessary however once Node.JS came along.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: how's the best way to do it JS auto load..

Post by Jackolantern »

I still have interest in node.js, but am going to wait a while for the information side of it to pick-up. For a rapidly up-and-coming platform, there is very little in the way of tutorials and other information out there. node.js's documentation is near useless, and trivial compared to most other tech's docs (like MSDN and Java Platform Docs). I am hoping things pick up in 2012, and hopefully the full, first-class support for Windows will bring it.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: how's the best way to do it JS auto load..

Post by Chris »

The thing about Node.JS is that you actually have to write JavaScript the way it was designed to be written, there aren't many frameworks other than express and socket.io. There is nothing like jQuery that simplifies the writing style. I would recommend learning the small amount of knowledge you actually need about the ECMA Standard once you get the swing of jQuery. It makes it really easy to understand the concepts of Node.JS. Imagine Node.JS as nothing else other than a really clever person. Someone who wouldn't start a conversation in real life, but once you ask or tell them something, they can keep the conversation going the way they want. It sends requests and information when it wants. To be able to achieve this an event style programming is needed, these events are performed when data has been manipulated or regenerated. Rather than a human clicking a button or moving their mouse to perform an event, like the interaction between the view and controller in an MVC framework, the server can trigger these events by controlling data in loops.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: how's the best way to do it JS auto load..

Post by Jackolantern »

The thing is, I actually know Javascript and jQuery really well, but without at least a decent tutorial, I just struggled when I tried to play around with node on my own since I don't know what all is available in the "layer" that node.js makes up on top of V8. And then, again without decent tutorials, it is hard to figure out how to organize a large socket.io or web application in a Javascript file (at least for me). PHP, ASP.NET MVC, JSF and everything thing else I have worked with break everything down into individual files and/or classes, whereas I don't see how node.js applications are broken down. Most of the examples I have seen are tiny. How would you organize an entire, enterprise web application is node? Putting hundreds of callbacks in one file would drive anyway mad!
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”