javascript 如何获得实时更新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6304937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get realtime updates?
提问by Hyman Billy
I was chatting frequently in Facebook and as I love to code more and more so I tried to create a Chat Script! Here it is ::: http://wooflux.co.cc/ChatSystem.1.1/ChatSystem.1.1/
我经常在 Facebook 上聊天,因为我越来越喜欢编码,所以我尝试创建一个聊天脚本!这是 ::: http://wooflux.co.cc/ChatSystem.1.1/ChatSystem.1.1/
You try it out. But it is just a beta version so there are some bugs in it. And one of the biggest bug is that it requires a good internet speed to get real time updates. But when I push my net's speed to it's maximum and then chat with one of my friends in Facebook, it work in real time! I was wondering how did they do that? Can anyone explain me how they achieved this. Currently I'm sending Ajax requests in number intervals to get real time update. So please help me out by explaining or giving a link that how did Facebook achieved real time Chats without using much of the Internet speed?
你试试看。但它只是一个测试版,所以它有一些错误。最大的错误之一是它需要良好的互联网速度才能获得实时更新。但是,当我将网络速度推到最大,然后在 Facebook 上与我的一位朋友聊天时,它会实时工作!我想知道他们是怎么做到的?谁能解释一下他们是如何实现这一目标的。目前我正在以数字间隔发送 Ajax 请求以获得实时更新。所以请通过解释或提供一个链接来帮助我解决 Facebook 如何在不使用太多互联网速度的情况下实现实时聊天?
回答by Raynos
To do real time communication you need a proper connection.
要进行实时通信,您需要正确的连接。
You can use WebSocketsto give you a real Browser - Server TCP connection.
您可以使用WebSockets为您提供真正的浏览器 - 服务器 TCP 连接。
The alternative to WebSockets would be a flash bridge (which uses websockets in flash) or COMETtechniques.
WebSockets 的替代方案是 flash 桥接器(在 flash 中使用 websockets)或COMET技术。
My personal recommendation is a WebSocket abstraction like socket.io.
我个人的建议是像socket.io 这样的 WebSocket 抽象。
Socket.io builds on node.jswhich Serverside Javascript. It excels at evented asynchronous real time communication.
Socket.io 建立在node.js之上,其中服务器端 Javascript。它擅长于事件异步实时通信。
If your going down the node route you might aswell pick up now
to make your life easy. It has a screencast about making a chat server in 12 lines.
如果您沿着节点路线走下去,您也可以选择now
让您的生活更轻松。它有一个关于在 12 行中制作聊天服务器的截屏视频。
回答by Rufinus
I don't know how Facebook does it, but we use Node.js
for pushing. Visit http://www.no-margin-for-errors.com/blog/2010/07/26/deliver-real-time-information-to-your-users-using-node-js/for an example.
我不知道 Facebook 是怎么做的,但我们Node.js
用于推送。访问http://www.no-margin-for-errors.com/blog/2010/07/26/deliver-real-time-information-to-your-users-using-node-js/获取示例。
回答by Jay Sidri
Use COMETto push your messages to the client instead of polling your server all the time
使用COMET将您的消息推送到客户端,而不是一直轮询您的服务器
请参阅:在 PHP 中使用 Comet?