`
- 浏览:
1221995 次
-
HTML5 WebSocket 通讯原理(Java实现)
在javaeye都好久了.但是从来没发表个什么东西由于最近在观注 HTML5 于是就看了下WebSocket又不想找现成的框架去搞.就打算自己弄弄.开始吧.提前条件1> 了解socket.2> 了解协议,了解HTTP协议更好.(想想为什么需要协议就行.安全?保证数据完整?便于解析?)3> 理解字节,字节序,如: 32位int 的 30转成 高字节序的字节 及是 0x00 0x00 0x00 0x1E,低字节序则 0x1E 0x00 0x00 0x00. 没数错的话是4个字节^_^Web Socket参考文章 1. http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 因为感觉里面有点乱,顺序没有按常理出牌. 下面是按我觉得应该有的顺序截取了该文章关键部分 The following diagrams summarise the protocol: Handshake | V Frame type byte <-------------------------------------. | | | | `-- (0x00 to 0x7F) --> Data... --> 0xFF -->-+ | | `-- (0x80 to 0xFF) --> Length --> Data... ------->-' 一个 WebSocket 通讯流程 简单说.就是先握手.在谈话.(跟见到领导一样)Internet-Draft The WebSocket protocol August 2010/*请求部分也就是浏览器传到服务器的数据,需要解析*/ GET /demo HTTP/1.1 Host: example.com Connection: Upgrade Sec-WebSocket-Key2: 12998 5 Y3 1 .P00 Sec-WebSocket-Protocol: sample Upgrade: WebSocket Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5 Origin: http://example.com ^n:ds[4U The handshake from the server looks as follows: /*响应部分 需要根据 请求消息 来生成响应报文*/ HTTP/1.1 101 WebSocket Protocol Handshake Upgrade: WebSocket Connection: Upgrade Sec-WebSocket-Origin: http://example.com Sec-WebSocket-Location: ws://example.com/demo Sec-WebSocket-Protocol: sample 8jKS'y:G*Co,Wxa- //上面request/response部分的以第二行至倒数第三行 都是这种格式,并且顺序无关 After the leading line in both cases come an unordered ASCII case- insensitive set of fields, one per line, that each match the following non-normative ABNF: [RFC5234] field = 1*name-char colon [ space ] *any-char cr lf colon = %x003A ; U+003A COLON (:) space = %x0020 ; U+0020 SPACE cr = %x000D ; U+000D CARRIAGE RETURN (CR) lf = %x000A ; U+000A LINE FEED (LF) name-char = %x0000-0009 / %x000B-000C / %x000E-0039 / %x003B-10FFFF ; a Unicode character other than U+000A LINE FEED (LF), U+000D CARRIAGE RETURN (CR), or U+003A COLON (:) any-char = %x0000-0009 / %x000B-000C / %x000E-10FFFF ; a Unicode character other than U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) NOTE: The character set for the above ABNF is Unicode. The fields themselves are encoded as UTF-8. Lines that don't match the above production cause the connection to be aborted. /****************重点部分******************/ To prove that the handshake was received, the server has to take three pieces of information and combine them to form a response. The first two pieces of information come from the |Sec-WebSocket-Key1| and |Sec-WebSocket-Key2| fields in the client handshake: Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8 Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0 For each of these fields, the server has to take the digits from the value to obtain a number (in this case 1868545188 and 1733470270 respectively), then divide that number by the number of spaces characters in the value (in this case 12 and 10) to obtain a 32-bit number (155712099 and 173347027). These two resulting numbers are then used in the server handshake, as described below. with 0x0D 0x0A and followed by 8 random bytes, part of a challenge, and the server sends 18 bytes starting with 0x0D 0x0A and followed by 16 bytes consisting of a challenge response. The details of this challenge and other parts of the handshake are described in the next section. The concatenation of the number obtained from processing the |Sec- WebSocket-Key1| field, expressed as a big-endian 32 bit number, the number obtained from processing the |Sec-WebSocket-Key2| field, again expressed as a big-endian 32 bit number, and finally the eight bytes at the end of the handshake, form a 128 bit string whose MD5 sum is then used by the server to prove that it read the handshake. 大概意思就是说 服务端需要根据 Sec-WebSocket-Key1,Sec-WebSocket-Key2和请求响应部分的最后8个字节 生成一个16个字节的数组(128位) 再对他进行MD5 签名也就是响应消息最后的 8jKS'y:G*Co,Wxa-实际上是根据 Sec-WebSocket-Key2: 12998 5 Y3 1 .P00 Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5 ^n:ds[4U //请求响应部分的最后8个字节 计算出来的规则:将的key1值部分"4 @1 46546xW%0l 1 5" 取掉非数字的字符,会得到一个数字 则: 414654015 在计算该字符串的空格数 假设为 n 让 int key1 = (414654015 / n); 然后将他转成 高字节序的字节数组 伪代码:public byte[] formatKey(String key);main{byte[] bytes = new byte[16];bytes[0~3] = formatKey(key1);bytes[4~7] = formatKey(key2);bytes[8~15] = 请求响应部分的最后8个字节bytes = md5(bytes);} /***********************************************/ 接下来的就简单了===发消息 This wire format for the data transfer part is described by the following non-normative ABNF, which is given in two alternative forms: the first describing the wire format as allowed by this specification, and the second describing how an arbitrary bytestream would be parsed. [RFC5234] //大概意思就是说有两种数据通讯协议,第一种简单的文本,也就是源码里用的,第二种就是二进制数据通讯协议.. ****传输之前必须先握手**** ; the wire protocol as allowed by this specification frames = *frame frame = text-frame text-frame = (%x00) *( UTF8-char ) %xFF ; the wire protocol including error-handling and forward-compatible parsing rules frames = *frame frame = text-frame / binary-frame text-frame = (%x00-%x7F) *( UTF8-char / %x80-%x7E ) %xFF binary-frame = (%x80-%xFF) length < as many bytes as given by the length > length = *(%x80-%xFF) (%x00-%x7F) 遵循他的格式. 照着socket 收发消息那样发就OK了!如:发一个 Hello!byte[] data = new byte[2+];byte[0] = 0x00byte[1~data.length - 1] = "Hello!".getBytes("UTF-8");byte[data.length - 1] = 0xFF//源码运行方式1.WebServerSocket.main();2.打开 socket.html (用Google Chrome)我的环境: WindowsXP,JDK1.6 , Google Chrome(version 13.0.782.220 m)
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
基于html5websocket+java实现简单通讯
基于html5websocket java实现简单通讯 index.jsp在输入框输入1234id5发送就可以发送给index1.jsp index1.jsp在输入框输入12332234id4发送就可以发送给index.jsp id前的消息随意,后面跟着要发送目标的id即可
Java WebSocket编程,java实现websocket,Java源码
ios 实现websocket和h5 websocket通讯,实现双向联通。
java 基于websocket实现的视频聊天,解压项目到对应位置部署到tomcat8即可使用
java写的websocket客户端,包含心跳监测。 此架包可解决问题:系统使用websocket 访问远程上的实时数据,但是有时候会停止更新实时数据,只要重启了自己的系统,就会继续更新数据了,此包可以完美解决此问题。
使用Java-WebSocket开源框架开发Android端即时通讯功能。主要功能: 1、与websocket建立长连接 2、与websocket进行即时通讯 3、Service和Activity之间通讯和UI更新 4、弹出消息通知(包括锁屏通知) 5、心跳检测和重...
本工程为MyEclipse javaWeb工程,用于展示如何使用 HTML5 webSocket API实现即时通讯的功能。 本工程编码方式:UTF-8 功能说明: 1、本功能支持多人聊天,类似于http://www.htkaoyan.com/网站中的在线自询; 2、...
WebSocket协议的Java实现 WebSocket4J
java 基于websocket实现的视频聊天,解压项目到对应位置部署到tomcat8即可使用
HTML5 websocket 聊天 java tomcat8
对 WebSocket 的一个合适的类比是打电话。当打电话时,你通过拨号初始化一个电话呼叫。如果你尝试呼叫的人通过拿起话筒接受了呼叫,连接就已建立。当连接活跃时,如果乐意,双方可以同时说话(虽然并不推荐此种自由...
该文档详细讲解了websocket的原理、如何使用websocket开发等
主要介绍了java 实现websocket的两种方式实例详解,一种使用tomcat的websocket实现,一种使用spring的websocket,本文通过代码给大家介绍的非常详细,需要的朋友可以参考下
java源码:WebSocket协议的Java实现 WebSocket4J.rar
C#_WinForm实现WebSocket及时通讯,客户端定向推送。
基于HTML5+WebSocket+JAVA的棋牌游戏五子棋程序,IDEA完整工程包src\main\java\com\chanming\common,main\java\com\chanming\websockets
WebSocket协议的Java实现 WebSocket4J.7z