如何从html5 websocket中计算出有效负载大小 [英] how to work out payload size from html5 websocket

查看:279
本文介绍了如何从html5 websocket中计算出有效负载大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何判断websockets上的html5消息有效负载长度?

how can I tell an html5 message payload length over websockets?

我知道基本协议由一个操作码组成,然后长度由下一个1到8个字节,然后以下值是散列,然后其余的是有效负载!

Im aware the base protocol consists of an op code, then length is determine by the next 1 to 8 bytes, then the following values is the hashing and then the rest is payload!

我创建一个java服务器端应用程序,它将从html5客户端接收消息然后处理, iI可以处理最多256个字节的消息。

Im creating a java serverside application that will receivea message from a html5 client and then handle, iI can handle messages up to 256 bytes.

任何帮助甚至手动解决这个问题都很好(即如何使用确定有效负载的字节)

Any help to work this out even manually would be great (i.e how to habdle the bytes that determine the payload)

干杯

推荐答案

websocket框架的有效载荷长度有三种方式可以编码,这取决于有效载荷的长度:

There are three ways how the payload length of a websocket frame can be encoded, which depend on the length of the payload:


  1. 125或更少

  2. 126 -65535

  3. 65536 +

通过查看值可以告诉我使用哪种方式第二个字节的最后7位(第2个字节的第一位是mas)国旗)。

Which way is used can be told by looking at the value of the 7 last bits of the 2nd byte (the first bit of the 2nd byte is the masking flag).

当它低于126时,它是有效载荷长度。
当它为126时,有效载荷长度在以下两个字节中。
当它为127时,有效载荷长度为以下8个字节。

When it's below 126, it's the payload length. When it's 126, the payload length is in the following two bytes. When it's 127, the payload length is in the following 8 bytes.

因此,您必须遵循以解释websocket框架的算法是:

So the algorithm you have to follow to interpret a websocket frame is:


  1. 读取第1个字节( byte0 )以获取fin标志和操作码

  2. 时读取第二个字节( byte1

  3. byte1 大于127,从 byte1 中减去127并将 masked 设置为 true

  4. byte1 现在为126时,读取接下来的两个字节并将其转换为a 。这是 payload_length

  5. byte1 为127时,请阅读下一页八个字节并将其转换为 long 。这是 payload_length

  6. byte1 是其他的时候, payload_length 等于 byte1

  7. 的值 masked 为true,读取下一个4字节。这是 masking_key

  8. 读取 payload_length 获取有效负载。

  9. 蒙面为真时,应用 masking_key 到有效载荷。

  1. read the 1st byte ("byte0") to get the fin flag and the opcode
  2. read the 2nd byte ("byte1")
  3. when byte1 is greater than 127, subtract 127 from byte1 and set masked to true
  4. when byte1 is now 126, read the next two bytes and convert it to a short. This is the payload_length.
  5. when byte1 is 127, read the next eight bytes and convert it to a long. This is the payload_length.
  6. when byte1 is something else, the payload_length is equal to the value of byte1
  7. when masked is true, read the next 4 byte. This is the masking_key.
  8. read the number of bytes you have in the payload_length to get the payload.
  9. when masked is true, apply the masking_key to the payload.

在websocket框架中没有哈希码,你必须混淆一些东西。 websocket框架的完整性由其下面的TCP层确保。

There is no hash-code in a websocket frame, you must have mixed something up. The integrity of a websocket frame is ensured by the TCP layer beneath it.

有关Websocket协议的更多信息,请参阅 RFC 6455

For more information about the Websocket protocol, refer to the RFC 6455.

这篇关于如何从html5 websocket中计算出有效负载大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆