Websocket 和 TCP [英] Websocket and TCP

查看:64
本文介绍了Websocket 和 TCP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Websocket 是一种基于消息的协议,使用 TCP 作为传输层.但 TCP 本身是一种基于流的协议.谁能解释一下

  1. 如何在 TCP 中分段 websocket 消息?
  2. Websocket 中的框架是什么?
  3. 在 Websocket 中使用消息有什么好处?我看到了 此处为什么 WebSockets 是基于帧而不是基于流的?

如果我有任何误解,请纠正我.

解决方案

要通过 TCP 发送特定数据格式(如 webSocket 帧),您只需定义数据格式,以便流的阅读器知道帧的开始位置并根据格式结束.一种典型的方法是发送一组包含固定大小(预先已知)标头格式的数据,其中包含总帧的长度,然后发送该长度的帧的其余部分.然后读取器可以读取初始固定大小的数据,从中读取长度,然后知道要读取多少才能获得整个帧.

<块引用>

Websocket 中的框架是什么?

您可以在这里阅读整个 webSocket 框架 下面是一个 webSocket 框架的结构.

帧格式:0 1 2 30 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1+-+-+-+-+-------+-+------------+-----------------+|F|R|R|R|操作码|M|有效载荷长度 |扩展的有效载荷长度 ||I|S|S|S|(4) |A|(7) |(16/64) ||N|V|V|V||S||(如果有效载荷 len==126/127) |||1|2|3||K|||+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +|扩展有效载荷长度继续,如果有效载荷 len == 127 |+ - - - - - - - - - - - - - - - +-------------------------------+||屏蔽键,如果 MASK 设置为 1 |+-------------------------------+-------------------------------+|掩码键(续) |有效载荷数据 |+-------------------------------- - - - - - - - - - - - - - - - +:有效载荷数据继续......:+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +|有效载荷数据继续... |+---------------------------------------------------------------+

<块引用>

在 Websocket 中使用消息有什么好处?

不清楚你在问什么.使用 webSocket 连接的原因包括:

  1. 能够随时将数据从服务器推送"到客户端.
  2. 随时从客户端向服务器或服务器向客户端发送数据的开销非常低的方式(重复数据包的开销比 http 请求低得多).
  3. 拥有持久连接的能力,您可以通过该连接执行许多操作,而无需使所有内容都符合 http 请求/响应格式.

According to my knowledge, Websocket is a message-based protocol and using TCP as the transport layer. But TCP itself is a stream-based protocol. Can someone explain

  1. how websocket message in segmented in TCP?
  2. what is framing in Websocket?
  3. what's advantage of using message in Websocket? I saw from here asking that Why are WebSockets frame-based and not stream-based?

Correct me if I have any misconception.

解决方案

To send a specific data format like a webSocket frame over TCP, you just have to define the data format so that the reader of the stream knows where the frame starts and ends based on the format. One typical way is to send a set of data that includes a fixed size (known in advance) header format that includes a length of the total frame and then you send the rest of the frame that is that length. The reader can then read the initial fixed size data, read the length out of that and then know how much more to read to get the whole frame.

what is framing in Websocket?

You can read about the whole webSocket frame here and here's a look at how a webSocket frame is structured.

Frame format:  
​​
      0                   1                   2                   3
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
     +-+-+-+-+-------+-+-------------+-------------------------------+
     |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
     |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
     |N|V|V|V|       |S|             |   (if payload len==126/127)   |
     | |1|2|3|       |K|             |                               |
     +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
     |     Extended payload length continued, if payload len == 127  |
     + - - - - - - - - - - - - - - - +-------------------------------+
     |                               |Masking-key, if MASK set to 1  |
     +-------------------------------+-------------------------------+
     | Masking-key (continued)       |          Payload Data         |
     +-------------------------------- - - - - - - - - - - - - - - - +
     :                     Payload Data continued ...                :
     + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
     |                     Payload Data continued ...                |
     +---------------------------------------------------------------+

what's advantage of using message in Websocket?

It's unclear what you're asking with this. Reasons for using a webSocket connection include the following:

  1. The ability to "push" data from server to the client at any time.
  2. Very low overhead way to send data from either client to server or server to client at any time (much lower overhead for repeated packets than an http request).
  3. Ability to have a persistent connection over which you can do many things without having to conform everything to the http request/response format.

这篇关于Websocket 和 TCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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