这个是我解析 websocket 的代码,现在有一个小缺陷还不是很完善,当数据长度 大于 125 的时候就解析不出来了。 由于本人在二进制的运算上非常薄弱,所以在此请教一下大家。还望指点迷津。
// 这句代码可以获取到客户端传来的数据
byte[] bytesData = outputStream.toByteArray();
if (bytesData.length < 1) {
return null;
}
int opcode = bytesData[0] & 0x0f;
if (opcode == 8) {
webSocketExchange.setWebSocketEnum(WebSocketEnum.CLOSE);
return webSocketExchange;
}
if (bytesData.length < 2) {
return null;
}
int payloadLength = (bytesData[1] & 0x7f);
if(payloadLength < 1){
return null;
}
if(payloadLength == 126){
// TODO 需要研究下怎么获取数据长度
} else if(payloadLength == 127){
// TODO 需要研究下怎么获取数据长度
}
if(bytesData.length < (payloadLength + 6)){
return null;
}
byte[] mask = Arrays.copyOfRange(bytesData, 2, 6);
byte[] payloadData = Arrays.copyOfRange(bytesData, 6, payloadLength + 6);
if(payloadData.length < payloadLength){
return null;
}
for (int i = 0; i < payloadData.length; i++) {
payloadData[i] = (byte) (payloadData[i] ^ mask[i % 4]);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(payloadData);
webSocketExchange.setLength(6 + outputStream.size());
webSocketExchange.setOutputStream(outputStream);
webSocketExchange.setWebSocketEnum(WebSocketEnum.MESSAGE);
1
jobs0 2021-06-25 21:32:19 +08:00
大佬,一点看不懂
|
2
0o0O0o0O0o 2021-06-25 21:36:12 +08:00 via iPhone
A. 看 websocket 的 RFC
B. 看别人的库源码 C. 看别人看别人的库源码后的总结文章 而从你的注释来看,仿佛你是靠抓包来猜的 |
3
Jirajine 2021-06-25 21:57:37 +08:00
datatracker.ietf.org/doc/html/rfc6455#section-5
Payload length: 7 bits, 7+16 bits, or 7+64 bits The length of the "Payload data", in bytes: if 0-125, that is the payload length. If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length. If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0) are the payload length. Multibyte length quantities are expressed in network byte order. Note that in all cases, the minimal number of bytes MUST be used to encode the length, for example, the length of a 124-byte-long string can't be encoded as the sequence 126, 0, 124. The payload length is the length of the "Extension data" + the length of the "Application data". The length of the "Extension data" may be zero, in which case the payload length is the length of the "Application data". |
4
Jirajine 2021-06-25 21:59:42 +08:00
|
5
Jirajine 2021-06-25 22:03:29 +08:00
另外,你这种写法看起来好像有点问题。不是很熟悉 Java,toByteArray()是不是分配了个新的 buffer 把整个流复制到里面?正常来说应该流式读取,需要解析多少就读多少,你这样可能会有性能损失。
|
6
liuxu 2021-06-25 22:29:01 +08:00
楼上都在说什么呢。。。
楼主需要的就是一个 byte to int 和 byte to long 的方法,一般用个包处理就行,自己写就是 0xabc = a * 16^2 + b * 16^1 + c * 16^0,32 位和 64 位自己算,注意是无符号 32 位或者 64 位整数 |
7
cyanray 2021-06-25 23:07:21 +08:00
确实只是个 byte to int 的问题。
以前用 C++写过 ws client: ```c++ if (info.PayloadLength == 126) { if (len < 4) return -1; info.PayloadLength = ((int64_t)frame_data[2] << 8) | frame_data[3]; offset = 4; } else if (info.PayloadLength == 127) { if (len < 10) return -1; memcpy(&info.PayloadLength, &frame_data[2], sizeof(uint64_t)); offset = 10; } if (info.Mask) { memcpy(&info.MaskKey, &frame_data[offset], sizeof(info.MaskKey)); offset += 4; } ``` |
8
MarkLeeyun 2021-06-26 10:10:39 +08:00
我还特意去了解了一下什么是 websocket..哈哈哈哈哈。
|
9
muzuiget 2021-06-27 03:38:16 +08:00
我写过 Lua 解析 WebSocket 库,看了下我的代码。
当 payloadLength < 126 时,这就是 payloadLength 当 payloadLength === 126 时,读接下来的 2 个字节作为新的 payloadLength 当 payloadLength === 127 时,读接下来的 8 个字节作为新的 payloadLength 大概是 int i = 1; // 当前读取的字节下标 int payloadLength = (bytesData[i] & 0x7f); if(payloadLength < 1){ return null; } if(payloadLength == 126){ payloadLength = bytesData.getInt16(i) i += 2; } else if(payloadLength == 127){ payloadLength = bytesData.getInt64(i) i += 8; } getInt16/getInt64 我瞎写的,你自己有没有类似方法。 |
10
Joker123456789 OP |