Java中的WebSocket Server(hybi 10)发送和接收
我正在为草案的最后一个版本制作一个 Java 服务器。我设法建立了连接,效果很好。
问题是我不明白数据是如何编码的,我一直在试图找到一些如何完成它的示例,但我找不到任何东西。所以我想自己做,但需要一些帮助。
这里是一个图像框架。
但我不明白有效负载从哪里开始。什么是:
扩展有效负载长度 (16/63)(如果有效负载 len==126/127)
这就是我的有效负载应该在的地方?
有人可以提供一些帮助吗,因为正如你所看到的,我完全迷失了......
I'm makinga Java server for the last version of the draft. I managed to make the connection, and that's working great.
The problem is that I don't understand how the data is encoded, I've been trying to find some example of how it has to be done but I couldn't find anything. so I'm trying to do it by myself but need some help.
Here is an image of the frame.
But I don't understand where the payload begins. What is:
Extended payload length (16/63) (if payload len==126/127)
That's the place where my payload should be?
Can someone give some help because as you may see, I'm completely lost...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是长度并不总是适合 7 位(您只能用 7 位表示数字 0 到 127),在这种情况下,将使用以下 2 或 8 个字节来使长度适合:
因此,如果未编码,有效负载将从索引 2、4 或 10 开始。编码时,它从 6、8 或 14 开始(因为有 4 个掩码字节)。
我之前发布了一些伪代码 关于解码有效负载数据。
要实际获得“实数”长度(而不是单独的字节),您可以使用按位移位运算符,如下所示(如果长度有两个字节):
这将按如下方式计算:
假设:
bytes[2]
为01101001
(105
以 10 为基数)bytes[3]
为10100101
(165
以 10 为基数)然后
<<
将会执行:|
基本上是将它们相加:所以如果你有字节
105
和165
,那么它们代表的长度为27045
。The problem is that the length does not always fit in 7 bits (you can only express the numbers 0 to 127 with 7 bits), and in that case either the following 2 or 8 bytes will be used to make the length fit:
So the payload starts at either index 2, 4 or 10, if not encoded. When encoded, it starts at either 6, 8 or 14 (because there are 4 mask bytes).
I previously posted some pseudocode about decoding the payload data.
To actually get the length as a "real number" (instead of separate bytes), you can use bitwise shift operators as follows (in case there are two bytes for the length):
This will calculate it like this:
Suppose:
bytes[2]
is01101001
(105
in base 10)bytes[3]
is10100101
(165
in base 10)Then
<<
will be doing:|
is basically adding them:So if you have the bytes
105
and165
, then they represent a length of27045
.