HTML5 Web Sockets

HTML5 Web Sockets

相關資料

具體介紹:http://www.html5rocks.com/zh/tutorials/websockets/basics/

瀏覽器支持版本:http://stackoverflow.com/questions/1253683/what-browsers-support-html5-websocket-api

什麼是 WebSockets

WebSockets是HTML5提供的一個支持客戶端和服務器之間進行實時通訊數據推送的非HTTP通信機制。使用Web Sockets 可以在服務器和客戶端之間建立一個非HTTP的雙向連接。Web Sockets在不被關閉的條件下,這個連接是實時的、永久的。這樣的機制實現了服務器不再需要輪詢客戶端的請求,從被動轉爲了主動。

WebSockets並非HTTP但他也帶有HTTP的開銷,所以在使用Web Sockets的時候並不適用於低延遲應用。

WebSockets的使用

1.創建websockets對象(將url作爲參數交給websockets


varwebSocket = new WebSocket(“ws://localhost:8005/socket”);


請注意 ws:。這是 WebSocket連接的新網址架構。對於安全 WebSocket 連接還有wss:,就像 https: 用於安全 HTTP 連接一樣。


2.與服務器通信

webSockets.send(data);//發送數據

webSockets.onmessage= function(ev){ var data = ev.data;}//接收數據


在通信過程中webSockets主要包含了四個狀態:

1.CONNECTING(數字值爲0),表示正在連接

2.OPEN(數字值爲1),表示已經建立好連接

3.CLOSING(數字值爲2),表示正在關閉連接

4.CLOSED(數字值爲2),表示已經關閉連接

webSockets無論發送還是接收都只支持字符串類型,在需要傳輸JSON對象的時候,可以通過JSON的stringify方法和parse進行轉換。

在最新規範中,WebSocket 也可以接收二進制訊息。接收的二進制幀可以是 Blob或 ArrayBuffer 格式。要指定收到的二進制數據的格式,可將WebSocket 對象的 binaryType 屬性設爲“blob”或“arraybuffer”。默認格式爲“blob”(您不必在發送時校正 binaryType 參數)WebSocket 的另一個新增功能是擴展。利用擴展,可以發送壓縮幀、多路複用幀等。您可以檢查 open 事件後的 WebSocket 對象的 extensions 屬性,查找服務器所接受的擴展。截止 2012 年 2 月,還沒有官方發佈的擴展規範。


WebSockets的服務器支持

lNode.js

uSocket.IO

uWebSocket-Node

uws

lJava

uJetty

lRuby

uEventMachine

lPython

upywebsocket

uTornado

lErlang

uShirasu

lC++

ulibwebsockets

l.NET

uSuperWebSocket



客服端服務器詳細支持情況

Client side

·Hixie-75:

·Chrome 4.0 + 5.0

·Safari 5.0.0

·HyBi-00/Hixie-76:

·Chrome 6.0 - 13.0

·Safari 5.0.2 + 5.1

·iOS 4.2 + iOS 5

·Firefox 4.0 -support for WebSocketsdisabled. To enable itsee here.

·Opera 11 -with support disabled. To enable itsee here.

·HyBi-07+:

·Chrome 14.0

·Firefox 6.0 - prefixed:MozWebSocket

·IE 9 - viadownloadable Silverlightextension

·HyBi-10:

·Chrome 14.0 + 15.0

·Firefox 7.0 + 8.0 + 9.0 + 10.0 -prefixed:MozWebSocket

·IE 10 (from Windows 8 developerpreview)

·HyBi-17/RFC 6455

·Chrome 16

·Firefox 11

·Opera 12.10 / Opera Mobile 12.1

Any browser with Flash can support WebSocket using theweb-socket-jsshim/polyfill.

Seecaniusefor the current status of WebSocketssupport in desktop and mobile browsers.

See thetest reportsfrom the WS testsuite included inAutobahn WebSocketsfor feature/protocol conformancetests.


Server side

InJava/Java EE:

·Jetty 7.0supports it (very easy to use)V 7.5 supports RFC6455-Jetty 9.1supports javax.websocket / JSR 356)

·GlassFish 3.0(very low level and sometimescomplex), Glassfish 3.1 has new refactored Websocket Support which is moredeveloper friendlyV 3.1.2 supports RFC6455

·Caucho Resin 4.0.2(not yet tried)V 4.0.25 supports RFC6455

·Tomcat 7.0.27now supports itV 7.0.28 supports RFC6455

·JSR356 included in Java EE 7willdefine the Java API for WebSocket,but is not yet stable andcomplete. See Arun GUPTA's articleWebSocket and Java EE 7 -Getting Ready for JSR 356 (TOTD #181)andQCon presentation(from 00:37:36 to 00:46:53) for moreinformation on progress. You can also look atJavawebsocket SDK.

Someother Java implementations:

·Kaazing Gateway

·jWebscoket

·Netty

·xLightWeb

·Webbit

·Atmosphere

·Grizzly

·ApacheActiveMQV 5.6 supports RFC6455

·Apache CamelV 2.10 supports RFC6455

·JBossHornetQ

In C#:

·XSockets.NET

·SuperWebSocket

·Nugget

·Alchemy-Websockets

·Fleck

·SignalR

In PHP:

·Ratchet

·phpwebsocket.

·Extendible Web Socket Server

·phpdaemon

InPython:

·pywebsockets

·websockify

·gevent-websocket,gevent-socketioandflask-socketsbasedon the former

·Autobahn

·Tornado

In C:

·libwebsockets

InNode.js:

·Socket.io: Socket.io also has serverside portsfor Python, Java, Google GO, Rack

·sockjs: sockjs also has serverside ports forPython, Java, Erlang and Lua

·WebSocket-Node-Pure JavaScript Client & Server implementation of HyBi-10.

Vert.x (also known as Node.x): A node like polyglot implementationrunning on a Java 7 JVM and based on Netty with :

·Support for Ruby(JRuby), Java,Grovy, Javascript(Rhino/Nashorn), Scala, ...

·True threading. (unlike Node.js)

·Understands multiple networkprotocols out of the box including: TCP, SSL, UDP, HTTP, HTTPS, Websockets,SockJSasfallback for WebSockets

Pusher.comis a Websocket cloud serviceaccessible through a REST API.

DotCloudcloudplatform supports Websockets, and Java (Jetty Servlet Container), NodeJS,Python, Ruby, PHP and Perl programming languages.

Openshiftcloudplatform supports websockets, and Java (Jboss, Spring, Tomcat & Vertx), PHP(ZendServer & CodeIgniter), Ruby (ROR), Node.js, Python (Django &Flask) plateforms.

For other language implementations,see the Wikipedia article for more information.

The RFC for Websockets :RFC6455



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章