开发者

Client side message functionality

How the ilusion of causing site to change content from server side is done? Let the example be gmail chat or chat on facebook. Or even new message sign on stack overflow.

Is it done开发者_StackOverflow中文版 by http://en.wikipedia.org/wiki/Comet_(programming)) ?

Thanks for help


That sort of things is usually done with a block of JavaScript firing again and again according to a timer. It will check the state of the things in the database and adjust something in the markup. For instance, change the CSS class of some element to introduce a different color or a bold font, replace a picture with the one done in a brighter color etc. Quite simple really. No magic involved.


The client side has to 'poll' the server for changes. i.e. a timer based Ajax call that checks the server every 15 seconds for new data, and takes action based on the result.

very loose example:

setTimeout('checkMessages()',15000);

function checkMessages() {
    //using jquery
    $.get( .......... , function (data) { if (data == "newmsg") { $('#newmsgind').blink(); });
    setTimeout('checkMessages()',15000);
}

Web browsers don't really maintain a connection to the server. You pull a page and that's it. Ajax allows continuous asynchronous communication, but it's always the client that initiates.


If you really don't like the javascript approach, you can write a Java applet that works the way you seem to prefer, maintaining an open connection to the server. But that's a heavyweight solution to what is usually a lightweight problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜