Reputation: 1507
I'm making a web browser based real time game.
I'm using jQuery ajax calls to call a web service method for the client to update their speed & location via json data. Returned is a list of strings which contain data for the locations and speeds of all other players.
At the moment I'm not even passing any data and am returning an example List entry.
This is the call, made from javascript every 25ms:
$.ajax({
type: "POST",
url: "Default.aspx/CheckIn",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) { UpdateEntityList(response); }
});
This is the web service method:
[WebMethod]
public static List<String> CheckIn()//(double xPos, double yPos, double heading, double speed)
{
List<String> Entities = new List<string>();
Entities.Add("0|0|50|50|5|180");
return Entities;
}
Can anyone suggest how to fix the problem, or even a better way of achieving my goal altogether? (Since I've only been doing web dev for a couple of weeks, I don't know that this is the best way)
Thanks
Upvotes: 1
Views: 1330
Reputation: 81
change the dataType to 'text', and parse the resulting json manually with json_parse.js. It avoids using eval which is what's causing the leak.
$.ajax({
type: "POST",
url: "Default.aspx/CheckIn",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (response) { UpdateEntityList(response); }
});
Upvotes: 0
Reputation: 28917
About "better way of achieving my goal altogether", yes indeed!
The proof of concept of a full fledged browser game has already been successfully carried out in Google's implementation of Quake in HTML 5. What you'll want to do is rejig your technology to be more in line with the way HTML 5 works (or will work). For example, you might want to use Web Sockets instead of AJAX calls.
Additionally take a look through Google's code blog for an overview of this game implementation and the project homepage to get sample code and learn from it.
Do some research on HTML 5 implementation levels in the various browsers to know in advance where your game will work right now. (e.g. consider HTML tag support, media support, Canvas support, etc). Also see info and wiki from the WHATWG . Here's a nifty slide presentation I like to show for demo. These technologies are still in their infancy though, so be informed. Also the W3C specs and related info.
You might have something good going if you pull it off in the early days of HTML 5 rather than following up later. A while has passed since April 2010 when Google proved Quake in the browser. This technologic will only continue to progress and be more broadly implemented over time so I suggest that you go ahead and start experimenting in that direction.
Be amazed by Google Chrome Experiments (specific to the Chrome browser of course). These examples will give you some great ideas. Pay particular attention to those games already implemented and their features to use as a gauge for your own work.
Good Luck! And have fun while increasing your skill set too.
Upvotes: 2
Reputation: 263147
I'd wager that what we're all trying to say in our comments (and what @Marcel Korpel said in his answer) is that you're using the wrong technology to base your game on.
Basically, you're trying very hard to achieve something what we could do twenty years ago with a cheap 16-bit processor by issuing asynchronous requests to a web server whose primary purpose is, well, to serve documents and, to a relative extent, high-level applications. Needless to say, your code will run hundreds of interoperable, security-conscious layers away from the bare metal.
In other words, that won't work.
A real-time game such as a TPS should run primarily on the client and, if it needs network interaction, should do so using a protocol based on e.g. UDP rather than HTTP.
Read about Flash, Silverlight or even the XNA platform, but AJAX is probably not the answer to your question.
Upvotes: 2
Reputation: 21763
As others already said, you can't do an XMLHttpRequest
every 25 ms. I think it's very likely that the effect you perceive (browser eating a lot of memory without releasing it) is also due to this: because the many function calls (at best every 25 ms, but it's also very likely that the JavaScript engine cannot hold up to that; read this nice answer to see how those timers work) don't allow the garbage collector to free memory anymore.
Upvotes: 1