Search code examples
c#asp.netclient-serverobserver-patternapplication-state

Events through application scope to multiple clients?


First of all, I am creating a something like a client/server solution using a standard ASP.NET website - I do know this method is not adviced, and most people would love to scream "COMET!" or "HTML5 Sockets!" - but please don't ;-) !

What I am doing...

I am creating an MMORPG on a website.

I have several clients whom need to be in contact at the same time. This is done by a global object in the Application scope.

My problem

I need to invoke an event to several clients. For instance, when an attack has been performed, I need to update some graphics. The attack logic is resolved in the global object, but each of the clients has to respond to this.

Right now I do the following:

   fightTrace.Reciever.InvokeMoveEnded(this);
   fightTrace.FiredBy.InvokeMoveEnded(this);

(This is a kind of observer pattern)

What now happends is a race condition. The one who loads the page_load event will get both of these events, and the one who is not running them, will experience no changes in the UI.

So what is it I really want?

What I really need is some genuine and nice way to create an observer pattern through the application state. I need to send an event out to every "listener" which is in this case is a client, and then do some update.

One way to do this is some session-thing, with true/false.. But I would really like some better way!

Thanks!


Solution

  • If I understood your context correctly then whenever the state of your application state object is changed you want to synchronize all the clients of your applications. What you are forgetting here is the stateless behavior of HTTP protocol. Once a response is sent the connection is lost you need to send an HTTP request again to be served again. However you can emulate some thing using State Management and Ajax based short and timely updates to simulate a connected environment. However I've to utter the words which you don't want to hear. Not recommended.

    Instead what you can do is to save the state of application object and whenever a request comes serve the response based on updated state of your object. Any how a client has to initiate a request.