开发者

WCF Comet Implementation

I have a requirement that needs a real-time updates on the Web client (ASP.NET MVC). The only way I can turn around on it is that to implement the COMET technique (ServerPush/Reverse-AJAX) technique.

The scenario is that: User A save a message in different website client. Then, User B will automatically get the updates made by User "A" in different browser.

I actually finish the solution by this Architecture: ASP.NET MVC - did a jquery ajax (post) request (long-pooled) on the WCF. WCF - do some polling on the database (SQL Server) with the interval of 1 second. If new data has been added to the database, the polling is break with the data being returned on the client.

WCF COMET Met开发者_StackOverflowhod Pseudo Code:

 private Message[] GetMessages(System.Guid userID)
 {
      var messages = new List<Message>();
      var found = false;
      /* declare connection, command */
      while (!found )
      {
           try
           {
                /* open connection - connection.Open(); */
                /* do some database access here */
                /* close connection - connection.Close(); */
                /* if returned record > 0 then process the Message and save to messages variable */
                /* sleep thread : System.Threading.Thread.Sleep(1000); */
                found = true;
           }
           finally
           {
                /* for sure, incase of exception */
                /* close connection - connection.Close(); */
           }
       }
       return messages.ToArray();
 }

My concern and question is: Is it the best approach to do the polling technique in WCF (with 1 second interval)?

Reason: I maximized the use of database connection pooling and I am expecting that there is no issue on that technique.

Note: This is a multi-threaded implementation with the use of WCF given attributes below.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall), ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = true)]


I'd recommend using a dedicated realtime server (i.e. don't host the WCF service in IIS) or using a hosted service for realtime updates. As Anders says, IIS isn't all that great at handling multiple long-running concurrent requests.

I'd also suggest you look at using a solution which uses WebSockets with support for fallback solutions such as Flash, HTTP streaming, HTTP long-polling and then possibly polling. WebSockets are the first standardised method of full duplex bi-directional communication between a client and a server and will ultimately deliver a better solution to any such problems.

For the moment implementing your own Comet/WebSockets realtime solution is definitely going to be a time consuming task (as you may have already found), especially when building a public facing app where it could be accessed by users with a multitude of different browsers.

With this in mind the XSockets project looks very interesting as does the SuperWebSocket project.

.NET Comet solutions I know of are from FrozenMountain have a WebSync server for IIS. There is also PokeIn.

I've compiled a list of realtime web technologies that may also be useful.


nstead of polling the database cant you have an event sent when updating instead? Thats the way I've implemented Pub/Sub scenarios anyway and it works great.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜