3 回答
TA贡献1906条经验 获得超3个赞
function encodeWebSocket(bytesRaw){ var bytesFormatted = new Array(); bytesFormatted[0] = 129; if (bytesRaw.length <= 125) { bytesFormatted[1] = bytesRaw.length; } else if (bytesRaw.length >= 126 && bytesRaw.length <= 65535) { bytesFormatted[1] = 126; bytesFormatted[2] = ( bytesRaw.length >> 8 ) & 255; bytesFormatted[3] = ( bytesRaw.length ) & 255; } else { bytesFormatted[1] = 127; bytesFormatted[2] = ( bytesRaw.length >> 56 ) & 255; bytesFormatted[3] = ( bytesRaw.length >> 48 ) & 255; bytesFormatted[4] = ( bytesRaw.length >> 40 ) & 255; bytesFormatted[5] = ( bytesRaw.length >> 32 ) & 255; bytesFormatted[6] = ( bytesRaw.length >> 24 ) & 255; bytesFormatted[7] = ( bytesRaw.length >> 16 ) & 255; bytesFormatted[8] = ( bytesRaw.length >> 8 ) & 255; bytesFormatted[9] = ( bytesRaw.length ) & 255; } for (var i = 0; i < bytesRaw.length; i++){ bytesFormatted.push(bytesRaw.charCodeAt(i)); } return bytesFormatted; } function decodeWebSocket (data){ var datalength = data[1] & 127; var indexFirstMask = 2; if (datalength == 126) { indexFirstMask = 4; } else if (datalength == 127) { indexFirstMask = 10; } var masks = data.slice(indexFirstMask,indexFirstMask + 4); var i = indexFirstMask + 4; var index = 0; var output = ""; while (i < data.length) { output += String.fromCharCode(data[i++] ^ masks[index++ % 4]); } return output; }
TA贡献1806条经验 获得超8个赞
发送消息
包含数据类型的一个字节(以及一些超出普通服务器范围的附加信息) 包含长度的字节 如果长度不适合于第二个字节,则为两个或八个字节(然后,第二个字节是表示长度使用多少字节的代码)。 实际(原始)数据
1000 0001
129
0
如果 0 <= length <= 125
,您不需要额外的字节 如果 126 <= length <= 65535
,您需要另外两个字节,第二个字节是 126
如果 length >= 65536
,您需要额外的8个字节,第二个字节是 127
AND 1111 1111
255
).
bytesFormatted[0] = 129 indexStartRawData = -1 // it doesn't matter what value is // set here - it will be set now: if bytesRaw.length <= 125 bytesFormatted[1] = bytesRaw.length indexStartRawData = 2 else if bytesRaw.length >= 126 and bytesRaw.length <= 65535 bytesFormatted[1] = 126 bytesFormatted[2] = ( bytesRaw.length >> 8 ) AND 255 bytesFormatted[3] = ( bytesRaw.length ) AND 255 indexStartRawData = 4 else bytesFormatted[1] = 127 bytesFormatted[2] = ( bytesRaw.length >> 56 ) AND 255 bytesFormatted[3] = ( bytesRaw.length >> 48 ) AND 255 bytesFormatted[4] = ( bytesRaw.length >> 40 ) AND 255 bytesFormatted[5] = ( bytesRaw.length >> 32 ) AND 255 bytesFormatted[6] = ( bytesRaw.length >> 24 ) AND 255 bytesFormatted[7] = ( bytesRaw.length >> 16 ) AND 255 bytesFormatted[8] = ( bytesRaw.length >> 8 ) AND 255 bytesFormatted[9] = ( bytesRaw.length ) AND 255 indexStartRawData = 10 // put raw data at the correct index bytesFormatted.put(bytesRaw, indexStartRawData) // now send bytesFormatted (e.g. write it to the socket stream)
接收讯息
包含数据类型的一个字节。 包含长度的字节 如果长度不适合第二个字节,则增加两个或八个字节。 四个字节,它们是掩码(=解码键) 实际数据
1000 0001
129
1
secondByte AND 0111 1111
第二个字节 0111 1110
,或 126
,表示长度使用以下两个字节 第二个字节 0111 1111
,或 127
,表示长度使用以下八个字节
decodedByte = encodedByte XOR masks[encodedByteIndex MOD 4]
encodedByte
encodedByteIndex
0
. masks
secondByte = bytes[1] length = secondByte AND 127 // may not be the actual length in the two special cases indexFirstMask = 2 // if not a special case if length == 126 // if a special case, change indexFirstMask indexFirstMask = 4 else if length == 127 // ditto indexFirstMask = 10 masks = bytes.slice(indexFirstMask, 4) // four bytes starting from indexFirstMask indexFirstDataByte = indexFirstMask + 4 // four bytes further decoded = new array decoded.length = bytes.length - indexFirstDataByte // length of real data for i = indexFirstDataByte, j = 0; i < bytes.length; i++, j++ decoded[j] = bytes[i] XOR masks[j MOD 4] // now use "decoded" to interpret the received data
添加回答
举报