I am developing a small brows开发者_StackOverflower based game in asp.net. Think of a game room which has a capaticy of 22 players and players join the room by clicking a button. ( I am saving the number of players in the room in database) I need to call a method when the number of players in the room is 22. The problem is I don't know how to control the number of players in the room. I mean I think like there need to be a bacground code which has to run all the time at the server and that code controls the number and call the function. It's my first web project(school project) and I hope you all can help me.
Presumably you will be calling a web method each time a user enters or leaves a room, right? So you could just update the number of users in the room at that point (make sure you do appropriate locking), and if it is 22, call whatever code you want (since you're already on the server). You may also need to periodically have users ping the server to let you know they are still active, so that you can remove them from the room if they get disconnected or whatever.
You mean to show the number of players in the room to the end-users? You could use AJAX to query a file on the server that returns the number of players online and use it to update a <div>
or something. jQuery can help simplify the client side code.
At a very high level when a user tries to join the room, you'd check to see whether the room is already full.
It starts to get complicated when you start to answer the question of who is still in the room. Do you already know all the cases which would cause a person to drop out of a room?
The most obvious thing that comes to mind for me is to make the game fall inline with current games.
Have a server (asp.net) and clients (silverlight / flash maybe).
On the server you can manage whats going on by storing things in the application state or in the session state objects.
You can track players progress between sessions using a database (sql server maybe).
You'll have to be aware of things like the application lifecycle ... http://msdn.microsoft.com/en-us/library/ms178473.aspx
I would recommend you use either ashx (web handlers) or WCF for your server based requirements.
On the client (lets say you want to use silverlight because you can still use C#) you can then connect to your server and request the data.
This will help you visualise (i hope) how multiplayer gaming works online.
As suggested above you may find that AJAX works fine for your needs, ajax requests are usually lightweight but remember the full sourceo f your client would effectively be given to each player so you're open to hacking.
Since this is a simple school project though i'm sure u'd be fine.
精彩评论