問題描述
我編寫了一個小型 Web 應(yīng)用程序,它基本上是瀏覽器中的一個 JQuery 支持的聊天客戶端,為了獲取我正在使用 AJAX 請求輪詢服務(wù)器然后附加任何新回復(fù)的帖子,我很擔心這在不失去實時感的同時盡可能高效.
I've written a small web application which is basically a JQuery powered chat client within the browser, to get the posts I'm polling the server with an AJAX request and then appending any new replies, I'm worried about making this as efficient as possible while not losing the realtime feel.
http://darklightweb.co.uk/RealTime/
我看不到任何可能發(fā)生中斷的方式,因此我每 5 秒輪詢一次頁面,如果沒有可用的新帖子以保持數(shù)據(jù)傳輸處于空閑狀態(tài),則該頁面不返回任何內(nèi)容,如果它確實有消息頂部隊列中的消息被發(fā)送出去,我會在 Ajax 請求完成后再次檢查,直到消息隊列為空.
I can't see any way that interrupts are possible so I am polling a page every 5 seconds that returns nothing if no new posts are available to keep data-transfer down if it's idle, if it does have a message the top message in the queue is sent out and I'm checking again as soon as the Ajax request has finished until the message queue is empty.
關(guān)于使這個盡可能低帶寬或可能的替代實現(xiàn)的任何其他提示?
Any other tips on making this as low-bandwidth as possible or possible alternate implementations?
推薦答案
輪詢可能不是實現(xiàn)聊天的最佳解決方案 - 我建議看看 JQuery 的實現(xiàn) COMET 保持與客戶端的開放連接并從服務(wù)器關(guān)閉"推送更新,并且也是 相當可擴展.
Polling might not be the best solution for implementing a chat - I'd suggest taking a look at JQuery's implementation of COMET which keeps an open connection to the client and pushes updates from the server 'down' and is also quite scalable.
這篇關(guān)于Ajax 聊天輪詢帶寬效率的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!