views:

283

answers:

4

I am trying to create a simple board game (a kind of checkers), where users will be able to play online with each other using flex application as a client.

I am using django application to process the game on the server side. And I come across the problem, if one user made a move, I can send it to a server, but how do I let the opponent know about it?

The way I am thinking to do it is to create a timer and send requests to the server asking was opponents move done or not....But here we have 2 limitations:

1) Each client would produce big amount of requests (not sure how server will work if I have e.g 100 such clients)

2) If players will chose game with a time limit for example 5 minutes/per game it will be very important to show them situation on the board as soon as it changes (without a pause), but timer will send request only on timer event, so if for example I will chose tick interval to 5 seconds it will mean that 5 seconds another side will not be aware of the situation changes.

+2  A: 

One way is to use a TCP Socket from the client to connect back to your server. Have the client listen for data, and have the server send updates whenever needed. This may require firewall changes (to allow the port you'll be using) and a server which accepts multiple persistent client connections. This may only work for a fixed smallish number of clients, since if you are keeping multiple connections open it will incur some server overhead.

If you have firewall restrictions and need to use HTTP ports, you can investigate Comet implementations. What I proposed in the first paragraph is more or less the same thing - Comet just does it over HTTP and standardises some aspects of the communication.

Vinay Sajip
+2  A: 

Think of it this way. If you poll every 1 or 2 seconds, that should be quick enough not to be noticed by either client. A simple REST request checking for changes is bloody quick and a modern web servers should be able to handle 100 such requests without issue.

Implement it with the timer now, run some performance tests and worry about servers after you're done.

If you are worried later, you can always have graduated timers. e.g., check after 100ms, 200ms, 400ms, 800ms, 1600ms, etc... with a cap at 5 seconds or something.

Take a look at this code for some ideas maybe, since chat uses similar concepts: http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/

Glenn
+2  A: 

The best would be to use AMF (Action Message Format) to communicate to the Flex application See PyAMF Django HowTo and in particular the links in the References section with examples.

Nathan
I just played with django and pyAMF. It's quite interesting thing. But as I understand it's just a way of making communications much faster, but anyway I will have to use timer, and check changes on the server each small period of time.
Oleg Tarasenko
A: 

Check out the messaging examples in Tour de Flex. They work with a variety of back-ends including BlazeDS. But I'm not sure if there is a Python implementation of messaging yet.

James Ward
hey james, for this example of chat in tour de flex, do you know if you have to have RMTP capable servers in order for the chat to work?
Rees
No. BlazeDS uses a streaming HTTP and long-polling HTTP channel instead of RTMP. There are open source products that have RTMP support (like Red5).
James Ward