开发者

Which is the best way to auto refresh a chat pag

开发者 https://www.devze.com 2023-01-17 07:19 出处:网络
I can easely use javascript to auto refresh the page every 10 sec for example but, isnt this going to overload the page having to ask for any change every 10 secons? is there a more efitient way to do

I can easely use javascript to auto refresh the page every 10 sec for example but, isnt this going to overload the page having to ask for any change every 10 secons? is there a more efitient way to do this. Some body told me once than there is a way having the client side waiting for a response from the server th开发者_Python百科at only sends it when it really has some change, for example some budy wrote a text in the chat. Is this really factible to do? is there a better way?.


Why refresh a chat page? Why not just check the server to see if there are new posts/chats etc. If so, append those to your chat box.

Have your timer (setTimeOut) fire say ever 5 seconds:

Quick pseudo code:

function updateChat()
{
  //Query a web service on the server for new text chat
  //If successful append the chat to the chat area

  //Call timeout again.
  setTimeout(updateChat, 500);
}

Edit: Seeing you have .NET I will assume you're using ASP.NET. Here's a link to a demo chat program:

http://trappedinhoth.blogspot.com/2009/04/ajax-jquery-chat-demo.html


You can apply outputcache on server side. IN this way server only need to generate 1 answer per each 10 seconds


You can try using the new WebSockets technology in JavaScript. It is not supported in many browsers (only Firefox 4, Chrome 4, and Safari 5 currently support it), but it allows the connection to the server to remain "open" and then the server can send a message to the client. More info:

  • http://en.wikipedia.org/wiki/WebSockets
  • http://dev.w3.org/html5/websockets/
  • http://www.websockets.org/
0

精彩评论

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