views:

397

answers:

2

I'm mostly looking for setup advise and pointers on how to go about going about this. I'll explain in as much detail as I can think and also note possible approaches that may be plausible.

The aim of this is to create a real time browser game, the best method that I have found for my needs would to use "long polling" with ajax, which will basically setup a request with the server that will "hang there" til the server has something to send it, then re-establish the connection upon receipt for more data. For my purposes this will handle a chat system aswell as character movement, IE: if a player enters the same area the clients there will recieve a response to inform them and thus update the browser client to show this.

The above is relatively easy to implement and I have already made a test-case for it, however I want to improve on it, on the server side it runs a loop for X amount of time before it'll auto timeout and send back and empty string, so another connection can be made, this is to prevent infinite loops and use up resources in cases where it shouldn't. Instead of looking up the database on each loop cycle (would be expensive I believe) for messages that need sending to the client, I use flatfiles, if a file has a modified timestamp greater than the last message sent to the client, then there is something new to send. However I believe this would also be expensive (not as much as using a mysql database though?) when done a couple of times per second.

My thought process on this was to have a C++ program (for speed) constantly running, and use that for very fast lookups in memory for new messages and so fourth, this would also give me the added bonus of being able to have bots within the game that the server can control for a more real-time feel/approach, however I have no clue if this is even possible and my searches on google have been fruitless.

The approach I would most love to be able to do, is to continue to use PHP to do the rendering and control of the page etc, and have the ajax requests go to the C++ application (that will always be running) that can handle all the real-time aspects.

CGI defeats the purpose of the above approach, as it creates a new instance of the application on each request, which is both slow and exactly what I do not want, I have php for that and don't want to switch one perfectally running language for another that would be better suited, PHP however (to my knowledge) can't store things in memory (ram) and so fourth.

Another approach that I have thought about was to use php sockets to connect into the C++ application, though I have no idea how feasible this may be. The C++ application only basically will need to control bots (AI) and the chat system messages.. I have absolutely no idea how to go about handling bots via PHP.

I hope this fully explains what my intentions and goals are, so if anyone has any pointers or advise then please reply and help me out, it would be very much appreciated. If you need any extra information (for if I didn't cover something or something very well) then I'll be happy to attempt to better explain.

+2  A: 

Reading your post sets alarm bells ringing.

How familiar are you with multi-threaded code? With C++? If the answer is "not very", then I fear you might be biting off a quite a large chunk. Why not take advantage of some existing (tried and tested) COMET server implementations rather than this barebones approach? Whatever application you have in mind, it should be quite separate from the comms implementation.

As someone who has implemented a such a server, I can tell you that it will take many design iterations and a helluva long time to get right. Testing such a product realisticly is also a very tricky process.

spender
I honestly didn't see a need to be fully multi-threaded, the reason for this is my experience with MUD games (Players would connect to those via telnet), the servers were not multi-threaded (only the DNS lookups), having players in their own thread I imagined would be overkill and hinder performance more than gains, as bobince (below) has mentioned that my main bottleneck will be the network.
Steve
If you set out to write a games server in C++ because it's fast, then you had better be familiar with multithreading/concurrency for dealing with the network IO side of things. There will be shared state that will, in all likelihood, be accessed at unpredictable times being driven by/driving network IO. A non-threaded approach would be... poor. I'm not advocating a player per thread, but the fact that IO happens concurrently is unavoidable and needs addressing if speed is important.
spender
You can perfectly well do asynchronous IO without threading. Whether it's the most appropriate or easiest way to code is arguable, but there are major network frameworks and servers that take that approach.
bobince
A: 

How fast do the reactions need to be? For anything approaching real-time action games, AJAX/Comet is going to be much too slow. The overhead is also really depressing.

The way forward for that kind of thing will probably be WebSocket, with a custom server on the backend. But I don't think that means you need to resort to C[++] for this; the bottleneck is most likely going to be the network and not server processor power.

I'm using a Python SocketServer with a trivial message replication system — all the game logic in my case is on the client-side, with some complicated JavaScript maintaining a consistent game world in the face of lag — but even for a more complex server-side I think a scripting language will probably be just fine.

WebSocket isn't ready yet; there are no mainstream browser implementations. In the meantime I'm using a Flash Socket backup that emulates the WebSocket interface. Flash Sockets have their own problems in that they fail to negotiate proxies, but they are fast and hopefully the need for them will diminish as WebSocket arrives properly.

bobince
I looked into comet and thought they were too limiting, i've seen the long polling work for an RPG game called Travians (It connected to an IP address on port 8010).The problem with the scripting language is that it isn't persistant (I believe that's the correct word? To elaborate it's not constantly running, so handling bot states with it would be an issue?)To further explain the travians approach, their long polling recieves a command string which the JS on the client further handles, for instance it'll recieve "MoveChar<X<Y" and then the JS handles the movement etc on the browser.
Steve
A scripting language can be used in a persistent way rather than merely as a web server backend. That's what I'm doing with Python and you can do it in any scripting language (even PHP, though it wouldn't be particularly suited). I too use one command string per line, although in my case they're lower-level commands like `.1 f` (left button pressed at game frame 15).
bobince
I have no experience with python, which is why I initially chose C++ for that area, would I have to go about using the php sockets and connect into the C++ application that way on each ajax request?
Steve
If you did it via the web server yes, but you don't generally want to be doing long-polling code through a vanilla web server+PHP setup, as you'll have many, many concurrent PHPs sitting wasting resources. If you're having a C++ socket server backend anyway, better to have the web clients connect directly to the socket server without the webserver/PHP layer. That would mean the C++ server would have to effectively be a web server of its own though, which is where being in a higher-level scripting language would make your life much easier.
bobince
Once you're at that point, you can also consider using a WebSocket and/or Flash Socket method of connecting to the socket server where available, since that'll be much more responsive than long-polling. The WS/Flash interface is also considerably easier for you to implement than a full-on AJAX/web server.
bobince
Excellent, thank you!
Steve