开发者

ASP.NET - Comet Pushing messages from server to all clients

开发者 https://www.devze.com 2023-01-12 13:11 出处:网络
I\'m making an application with server sided variables that change every second. Every second those new variable need to be shown at all the clients that have the webpage open.

I'm making an application with server sided variables that change every second. Every second those new variable need to be shown at all the clients that have the webpage open.

Now most people told me to go with comet because I need to push/pull the data every second, now I've got a few questions:

  • What would be a better solution looking at the fact that I need the new data EVERY SECOND, pulling from the client or pushing with the server?

  • Also the item ID's that are on the ser开发者_C百科ver side (with the variable's that ID got) can change and when the client refreshes the page he needs to get the oldest (and living) ID's. This would mean that my jquery/javascript on the client side must know which ID's he got on the page, what is best way to do this?

  • Last thing is that I can't find a good (not to expensive) comet library/api for asp.net (C#). Anyone ever used a comet library with good results? We're looking at a site that should be able to have 2000 comet connections at every moment.


There is a SendToAll function in PokeIn ASP.NET ajax library.


WebSync by Frozen Mountain is a full-fledged scalable comet server for IIS and ASP.NET. It integrates seamlessly into your application stack to support real-time data communications with thousands of clients per server node.

Check it out, there's a Community edition freely available.


What would be a better solution looking at the fact that I need the new data EVERY SECOND, pulling from the client or pushing with the server?

I don't think it doesn't matter that much, as the time between requests and the time new data will be available is rather short. I would just instantiate a new XMLHttpRequest at the client after the previous one succeeded. You could send the server the last received data (if not too big) so it can compare that data with the current one available on the server and only send something back when new data is available.

Also the item ID's that are on the server side (with the variable's that ID got) can change and when the client refreshes the page he needs to get the oldest (and living) ID's. This would mean that my jquery/javascript on the client side must know which ID's he got on the page, what is best way to do this?

I'm not totally sure I understand what you mean, but if I'm right you can just store every name/value pair in an object. When a new variable arrives at the client, it doesn't overwrite existing data; when a certain variable is already present, it is updated with the latest value. It could look like:

{ first_variable: 345,
  second_one: "foo",
  and_the_third: ["I", "am", "an", "array,", "hooray!"]
}

and when a new state of second_one arrives, e.g. "bar", the object is updated to:

{ first_variable: 345,
  second_one: "bar",
  and_the_third: ["I", "am", "an", "array,", "hooray!"]
}

Last thing is that I can't find a good (not to expensive) comet library/api for asp.net (C#). Anyone ever used a comet library with good results?

I don't have any experience with ASP.NET, but do you need such a library for this? Can't you just program the server-side code yourself, which, as I said, leaves the connection open and periodically (continually) compares the current state with the previous sent state?


UPDATE: To show it's not that difficult to keep a connection open at the server side, I'll show you a long-polling simulation I wrote in PHP:

<?php
  sleep(5);
?>
<b>OK!</b>

Instead of letting the process sleep a few seconds, you can easily test for changes of the state in a loop. And instead of sending an arbitrary HTML element, you can send the data back, e.g. in JSON notation. I can't imagine it would be that hard to do this in ASP.NET/C#.

0

精彩评论

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

关注公众号