問題描述
我們計劃使用 Websockets 技術制作一個基于 HTML5 的聊天應用程序.
We were planning to make an HTML5 based chat app using the Websockets technology.
所以我的問題是:
截至今天,哪些瀏覽器本身支持 Websockets?
如果瀏覽器不支持它,什么是可能的優雅回退?
有沒有可以提供幫助的 polyfill?
問候,
推薦答案
目前有哪些瀏覽器原生支持 Websocket?
Which are the browsers that support Websockets natively currently as of today?
正如之前的答案所指出的那樣.
As pointed out in previous answers.
見:
- http://caniuse.com/websockets
- http://html5please.com/#Websockets
如果瀏覽器不支持它,什么是可能的優雅回退?
If a browser does not support it, what is a possible graceful fallback?
如果您的實時網絡服務器僅支持 WebSockets,那么下一個最佳選擇是使用 web-socket-js 它是一個基于 Flash 的 polyfill,它創建了一個 WebSocket
對象,該對象以與原生 WebSocket
對象相同的方式進行交互.
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.
其他回退 - 需要由您的實時網絡服務器支持并由它提供的 JavaScript 客戶端庫管理 - 是從最好到最壞的順序:
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 - 來自服務器 -> 客戶端的單個流連接.可以使用第二個 HTTP 請求實現雙向通信.
- HTTP 流 - 使用兩個連接來模擬雙向連接.消息通過持久的流"連接服務器推送" -> 客戶端
- HTTP 長輪詢 - 也使用兩個連接.但是,服務器 -> 客戶端已打開,一直保持到新消息可用或發生超時.然后關閉它,如果設置了任何數據,它會在響應中傳遞.
- 標準 HTTP 輪詢 - 效率低得多,而且可能會浪費大量請求.但是,如果您的應用程序提供的更新沒有那么頻繁地發生,并且數據在輪詢請求之間的時間是否過時"也無關緊要,那么這可能是一個可以接受的解決方案.這里沒有推".
- 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.
選項 1. 和 2. 可以通過多種不同的方式實現,具體取決于網絡瀏覽器.這就是使他們成為黑客"的原因.我們現在有用于雙向通信的 WebSocket 和 EventSource(服務器發送的事件),它與 HTTP Streaming 非常相似,并且具有支持自動重新連接的額外好處.
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?
是的,請參閱前面提到的 web-socket-js.
Yes, see web-socket-js as previously mentioned.
對于 PHP,您的最佳選擇是 Ratchet.它不在 Apache 中運行,因此不會受到相同的限制 - 它沒有考慮到請求/響應范式.
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.
我現在看到的最常用的解決方案是:
The most commonly used solutions I see right now are:
- Faye - node.js 和 ruby??
- socket.io - node.js 和適用于各種其他語言的端口
- 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
對于其他選項 - 包括托管服務,如 Pusher(我在撰寫本文時為其工作) - 看看這個 實時網絡技術指南,我正在維護(并接受對其的貢獻).
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).
這篇關于用于實時聊天應用程序的 HTML5 Websockets?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!