Java中的WebSocket Server(hybi 10)发送和接收

发布于 2024-12-12 05:33:23 字数 424 浏览 0 评论 0原文

我正在为草案的最后一个版本制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-12-19 05:33:23

问题是长度并不总是适合 7 位(您只能用 7 位表示数字 0 到 127),在这种情况下,将使用以下 2 或 8 个字节来使长度适合:

  • 126 意味着接下来的 2 个字节用于长度
  • 127 意味着接下来的 8 个字节用于长度

因此,如果未编码,有效负载将从索引 2、4 或 10 开始。编码时,它从 6、8 或 14 开始(因为有 4 个掩码字节)。

我之前发布了一些伪代码 关于解码有效负载数据。


要实际获得“实数”长度(而不是单独的字节),您可以使用按位移位运算符,如下所示(如果长度有两个字节):

var length = (bytes[2] << 8) | (bytes[3] << 0);

这将按如下方式计算:

假设:

  • bytes[2]01101001105 以 10 为基数)
  • bytes[3]10100101165 以 10 为基数)

然后 << 将会执行:

01101001 00000000   // moved 8 places to the left, filled with zeroes
         10100101   // moved 0 places (nothing really happening, you can eliminate '<< 0')

| 基本上是将它们相加:

01101001 00000000
         10100101
-----------------  |
01101001 10100101      (in base 10 that's 27045)

所以如果你有字节 105165,那么它们代表的长度为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:

  • 126 means the following 2 bytes are used for the length
  • 127 means the following 8 bytes are used for the length

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):

var length = (bytes[2] << 8) | (bytes[3] << 0);

This will calculate it like this:

Suppose:

  • bytes[2] is 01101001 (105 in base 10)
  • bytes[3] is 10100101 (165 in base 10)

Then << will be doing:

01101001 00000000   // moved 8 places to the left, filled with zeroes
         10100101   // moved 0 places (nothing really happening, you can eliminate '<< 0')

| is basically adding them:

01101001 00000000
         10100101
-----------------  |
01101001 10100101      (in base 10 that's 27045)

So if you have the bytes 105 and 165, then they represent a length of 27045.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文