How does in-browser chat work?

别等时光非礼了梦想. 提交于 2019-12-03 04:07:12

问题


Just curious. How exactly does chatting in a browser work? Usually if a user goes to a web page, his/her webbrowser requests the page content. A server produces output and sends it to the user's computer. But with chatting it's a little bit the other way around (well not exactly). It's not the user requesting a chat message from some server, but rather the server that sends it directly. Now this is really simple to achieve with a "normal" server, but the thing that the server sends it to a browser directly confuses me. The posting the message part is all clear, it's simple. You just post the data to the server with for example ajax or something. But how does the other computer instantly "know" that a message has been written to it? It must obviously be the server sending it to the other computer as soon as it has been written. But somehow that doesn't compute in my brain. In my brain, the browser only request things, it doesn't just get them. How exactly do you do that?

Take google talk in gmail for example. How does that work? How is it implemented?


回答1:


There are some push technologies, such as Comet, but they're not widely implemented. Most of the time this is accomplished via polling at some small interval with AJAX and downloading any new messages that are available since the last downloaded message.




回答2:


Take a look at Comet




回答3:


Orbited is a good way to implement this, it uses comet methodologies. You can find a tutorial here.

Someone mentioned ajax polling, but comet is always better than polling. Well, that's just a sensationalist title, but comet is definitely more appropriate and can lead to less stress on the server side, with the right back-end.




回答4:


It uses AJAX - a client-side javascript running on the GMail user's browser sends and receives the messages from the Google server, and writes them to the browser window - no reload of the page required.




回答5:


I've develop a browser chat similar to facebook chat technology ( http://chatsign.com ). It is not a push technology but something better than polling, its call ajax long polling.

For more information about ajax long polling you can do some research or visit : http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery




回答6:


Modern chat applications use SSE's (Server Sent Events: a feature of html5) to send new messages to browsers



来源:https://stackoverflow.com/questions/1219532/how-does-in-browser-chat-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!