(另请参见:如何在服务器端发送和接收WebSocket消息?)
这很容易,但是了解格式很重要。
第一个字节几乎始终是
10000001,其中的
1意思是“最后一帧”,三个
0s是保留位,到目前为止没有任何意义
0001,也意味着它是一个文本框架(Chrome使用该
ws.send()方法发送)。
( 更新:
Chrome现在还可以发送带有的二进制帧
ArrayBuffer。第一个字节的后四位是
0002,因此您可以在文本数据和二进制数据之间进行区别。数据的解码方式完全相同。)
第二个字节包含一个
1(表示已被“屏蔽”(编码)),后跟七个代表帧大小的位。如果介于
000 0000和之间
1111101,则为大小。如果为
111 1110,则后面的2个字节为长度(因为它不适合7个位),如果为
1111111,则下面的8个字节为长度(如果不适合两个字节)。
接下来是四个字节,它们是解码帧数据所需的“掩码”。这是使用异或编码完成的,异或编码使用
indexOfByteInData mod4数据定义的掩码之一。解码简单工作方式类似
enpredByte xormaskByte(如果
maskByte是
indexOfByteInData mod 4)。
现在我必须说我完全没有C#经验,但这是一些伪代码(恐怕有些Javascript口音):
var length_pre = bytes[1] & 127, // remove the first 1 by doing '& 127' masks, data;if(length_pre === 126) { masks = bytes.slice(4, 8); // 'slice' returns part of the byte array data = bytes.slice(8); // and accepts 'start' (inclusively)} else if(length_pre === 127) { // and 'end' (exclusively) as arguments masks = bytes.slice(10, 14); // Passing no 'end' makes 'end' the length data = bytes.slice(14); // of the array} else { masks = bytes.slice(2, 6); data = bytes.slice(6);}// 'map' replaces each element in the array as per a specified function// (each element will be replaced with what is returned by the function)// The passed function accepts the value and index of the element as its// argumentsvar depred = data.map(function(byte, index) { // index === 0 for the first byte return byte ^ masks[ index % 4 ]; // of 'data', not of 'bytes' // xor mod});您还可以下载可能会有所帮助的规范(它当然包含了解格式所需的所有内容)。



