問(wèn)題描述
我們計(jì)劃使用 Websockets 技術(shù)制作一個(gè)基于 HTML5 的聊天應(yīng)用程序.
We were planning to make an HTML5 based chat app using the Websockets technology.
所以我的問(wèn)題是:
截至今天,哪些瀏覽器本身支持 Websockets?
如果瀏覽器不支持它,什么是可能的優(yōu)雅回退?
有沒(méi)有可以提供幫助的 polyfill?
問(wèn)候,
推薦答案
目前有哪些瀏覽器原生支持 Websocket?
Which are the browsers that support Websockets natively currently as of today?
正如之前的答案所指出的那樣.
As pointed out in previous answers.
見(jiàn):
- http://caniuse.com/websockets
- http://html5please.com/#Websockets
如果瀏覽器不支持它,什么是可能的優(yōu)雅回退?
If a browser does not support it, what is a possible graceful fallback?
如果您的實(shí)時(shí)網(wǎng)絡(luò)服務(wù)器僅支持 WebSockets,那么下一個(gè)最佳選擇是使用 web-socket-js 它是一個(gè)基于 Flash 的 polyfill,它創(chuàng)建了一個(gè) WebSocket
對(duì)象,該對(duì)象以與原生 WebSocket
對(duì)象相同的方式進(jìn)行交互.
If your realtime web server only supports WebSockets then the next best option is to use web-socket-js it's a Flash-based polyfill which creates a WebSocket
object which an be interacted with in the same ways as the native WebSocket
object.
其他回退 - 需要由您的實(shí)時(shí)網(wǎng)絡(luò)服務(wù)器支持并由它提供的 JavaScript 客戶端庫(kù)管理 - 是從最好到最壞的順序:
Additional fallbacks - which need to be supported by your realtime web server and managed by the JavaScript client library it provides - are, ordered best to worst:
- EventSource - 來(lái)自服務(wù)器 -> 客戶端的單個(gè)流連接.可以使用第二個(gè) HTTP 請(qǐng)求實(shí)現(xiàn)雙向通信.
- HTTP 流 - 使用兩個(gè)連接來(lái)模擬雙向連接.消息通過(guò)持久的流"連接服務(wù)器推送" -> 客戶端
- HTTP 長(zhǎng)輪詢 - 也使用兩個(gè)連接.但是,服務(wù)器 -> 客戶端已打開(kāi),一直保持到新消息可用或發(fā)生超時(shí).然后關(guān)閉它,如果設(shè)置了任何數(shù)據(jù),它會(huì)在響應(yīng)中傳遞.
- 標(biāo)準(zhǔn) HTTP 輪詢 - 效率低得多,而且可能會(huì)浪費(fèi)大量請(qǐng)求.但是,如果您的應(yīng)用程序提供的更新沒(méi)有那么頻繁地發(fā)生,并且數(shù)據(jù)在輪詢請(qǐng)求之間的時(shí)間是否過(guò)時(shí)"也無(wú)關(guān)緊要,那么這可能是一個(gè)可以接受的解決方案.這里沒(méi)有推".
- EventSource - A single streaming connection from server -> client. Bi-directional communication can be achieved using a second HTTP request.
- HTTP Streaming - uses two connections to simulate bi-directional connectivity. Messages are 'pushed' over a persistent 'streaming' connection server -> client
- HTTP Long-Polling - also uses two connections. However, the server -> client is opened, held until either a new message is available or a timeout occurs. It's then closed and if any data has been set it is delivered in the response.
- Standard HTTP polling - much less efficient and due to the large amount of potentially wasted requests. However, if the updates your app delivers don't happen all that frequently and it doesn't matter if data is 'out of date' for the time between polling requests, then this may be an acceptable solution. There is no 'push' here.
選項(xiàng) 1. 和 2. 可以通過(guò)多種不同的方式實(shí)現(xiàn),具體取決于網(wǎng)絡(luò)瀏覽器.這就是使他們成為黑客"的原因.我們現(xiàn)在有用于雙向通信的 WebSocket 和 EventSource(服務(wù)器發(fā)送的事件),它與 HTTP Streaming 非常相似,并且具有支持自動(dòng)重新連接的額外好處.
Options 1. and 2. can be achieved in numerous different ways depending on the web browser. This is what makes them 'hacks'. We now have WebSocket for bi-directional communication and EventSource (server-sent events) which is very similar to HTTP Streaming with the added benefit of it supporting auto-reconnection.
是否有 polyfill 可以提供幫助?
Is there a polyfill that can help?
是的,請(qǐng)參閱前面提到的 web-socket-js.
Yes, see web-socket-js as previously mentioned.
對(duì)于 PHP,您的最佳選擇是 Ratchet.它不在 Apache 中運(yùn)行,因此不會(huì)受到相同的限制 - 它沒(méi)有考慮到請(qǐng)求/響應(yīng)范式.
For PHP your best choice is Ratchet. It doesn't run within Apache so it doesn't suffer from the same limitations - it wasn't built with the Request/Response paradigm in mind.
我現(xiàn)在看到的最常用的解決方案是:
The most commonly used solutions I see right now are:
- Faye - node.js 和 ruby??
- socket.io - node.js 和適用于各種其他語(yǔ)言的端口
- SockJS - erlang、node.js、ruby
- SignalR - .NET
- XSockets - .NET
- Faye - node.js and ruby
- socket.io - node.js and ports available for various other languages
- SockJS - erlang, node.js, ruby
- SignalR - .NET
- XSockets - .NET
對(duì)于其他選項(xiàng) - 包括托管服務(wù),如 Pusher(我在撰寫(xiě)本文時(shí)為其工作) - 看看這個(gè) 實(shí)時(shí)網(wǎng)絡(luò)技術(shù)指南,我正在維護(hù)(并接受對(duì)其的貢獻(xiàn)).
For other options - including hosted services like Pusher (who I work for at the time of writing) - take a look at this realtime web tech guide which I'm maintaining (and accepting contributions towards).
這篇關(guān)于用于實(shí)時(shí)聊天應(yīng)用程序的 HTML5 Websockets?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!