您的位置:首页 > Web前端 > HTML5

HTML5 Web Sockets

2014-02-26 16:52 225 查看
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.jsuSocket.IOuWebSocket-NodeuwslJavauJettylRubyuEventMachinelPythonupywebsocketuTornadolErlanguShirasulC++ulibwebsocketsl.NETuSuperWebSocket

客服端服务器详细支持情况

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.1Any 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 friendly
V 3.1.2 supports RFC6455
·Caucho Resin 4.0.2(not yet tried)
V 4.0.25 supports RFC6455
·Tomcat 7.0.27now supports it
V 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·ApacheActiveMQ
V 5.6 supports RFC6455
·Apache Camel
V 2.10 supports RFC6455
·JBossHornetQIn C#:·XSockets.NET·SuperWebSocket·Nugget·Alchemy-Websockets·Fleck·SignalRIn PHP:·Ratchet·phpwebsocketExtendible Web Socket Server·phpdaemonInPython:·pywebsockets·websockify·gevent-websocket,gevent-socketioandflask-socketsbasedon the former·Autobahn·TornadoIn C:·libwebsocketsInNode.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 WebSocketsPusher.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

本文出自 “插上javaScript的翅膀” 博客,请务必保留此出处http://shimengwen.blog.51cto.com/2448562/1361421
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: