当前位置:网站首页>Espressif plays websocket
Espressif plays websocket
2022-07-23 05:38:00 【Embedded engineering lion】
This article is only for ESP32.
ESP32 Use WebSocket There are few scenarios for communication , Most of the application layer protocols used are MQTT and HTTP. But for the WebSocket, Basic understanding is still necessary . I've just had time recently , Just roll it WebSocket.
This article is just a simple description of WebSocket Analyze , If you want to know more about it , Suggestion or reference RFC6455.
List of articles
WebSocket What is it? ?
WebSocket It is a network communication protocol . Follow MQTT、HTTP agreement , It also belongs to TCP/IP Application layer protocol in the four layer model .
WebSocket The agreement 2008 The year was born ,2011 Became an international standard in .HTML5 Start offering a The network technology of full duplex communication between browser and server . It's based on TCP transmission , And reuse HTTP The handshake .
WebSocket And HTTP The relationship between ?
about HTTP, We all know that it has the following two characteristics :
- There is no connection : Every time HTTP The connection only handles one request . After the server returns the request from the client , This time HTTP Connecting means disconnecting . This feature can greatly improve the execution efficiency of the server .
- No state :HTTP Agreement for request / The status information is not saved in the response process . It means that if the subsequent processing requires the previous information , It can only be achieved by retransmission . This feature can reduce the memory burden of the server , Improve the response speed of the server .
These two features greatly improve the execution efficiency and response speed of the server in theory . But it can't be covered up HTTP One drawback of the agreement , That's it Communication can only be initiated by the client . This was no problem before , But now applications have higher and higher requirements for real-time , A typical application scenario is live broadcast , Webpage game , Stock trading, etc . Although it can pass polling The way to solve , But the efficiency of polling is very low , Non stop polling virtually increases the burden on the server , This is related to HTTP The original intention of the design runs counter .
See here , Some readers must have thought of HTTP Long connection of . you 're right , When one HTTP After the server responds to your request ,HTTP Based on TCP The connection won't be closed . If the client wants to send again HTTP Request , It can be done directly at the TCP Send on connection . Compare the way of polling , Long connection is an elegant way .HTTP The long connection of is to add a header field to the response header Connection:keep-alive To achieve . But long connections don't stay connected forever , There is also a holding time .HTTP The long connection of is essentially TCP Long connection of .
The same thing :
- All belong to TCP/IP Application layer protocol in the four layer model .
- All based on TCP.
- WebSocket And HTTP Good compatibility , The default port is also
80and443.
Difference :
HTTP Is one-way , It can only be initiated by the client Request request ;WebSocket It's two-way .contact :
WebSocket Just borrow HTTP To complete the handshake . After a successful handshake ,WebSocket It has its own frame for communication .
WebSocket And Socket The relationship between ?
about Socket, We all know that it is just an abstract concept , It will TCP/IP Layer operations are abstracted into several simple interfaces for application programs to call . For users , Just call a few socket Interface can complete the communication between hosts .
Socket It can also be used to represent the two ends of a connection in the network .WebSocket It just borrows this concept to represent the two ends of a connection in the network , These two ends can be regarded as browsers and servers .
WebSocket A simple example
Espressif Already available WebSocket Of demo. But in this demo The server used in is no longer providing services , So you can use a public server or set up a server locally . This paper chooses the latter .

build WebSocket Local server
In this paper Windows Building in the environment is based on nodejs Platform WebSocket Testing tools wscat.
Refer to the official instructions :wscat
Installation steps :
- install nodejs
- After installation, enter the following command under the command line to install globally wscat
npm install -g wscat
start-up WebSocket Server side
After installation , Enter the following command locally 8080 Port boot WebSocket service .
wscat -l 8080

start-up WebSocket client
stay WebSocket Execute the following commands under the directory to configure ( Set up WebSocket Of URL):
idf.py menuconfig

Download the firmware to ESP32 And run , You can see in the WebSocket The server continues to receive messages from ESP32 Sent from Text data .
WebSocket agreement
combination Wireshark Grab the bag , You can simply right WebSocket Protocol analysis .
handshake
establish TCP Connect
This need not be introduced too much ,TCP Establishing a connection requires
3The second handshake . package 23545 (SYN), package 23547 (SYN, ACK) And the bag 24983 (ACK) respectively TCP Of3Secondary handshake bag .handshake
WebSocket With the help of HTTP To complete the handshake . The client first needs to send a handshake packet , Shake hands with HTTP GET Request The structure of is sent to the server , After the server parses it, use HTTP Response Send the structure of to the client , among Response The status code in is
101Indicates a successful handshake .
stay WebSocket The protocol document has the following requirements for the handshake request package sent by the client :
1. The handshake request package must be a valid HTTP Request
2. Request Method It has to be for Get,Request Version Must be at least 1.1 edition
3. The request header must contain a Host Key value pair , If not used in the request header port If the key value is right , That's in Host The corresponding value must indicate the port number
4. The request header must contain a Upgrade Key value pair , Value must be websocket
5. The request header must contain a Connection Key value pair , Value must be Upgrade
6. The request header must contain a Sec-WebSocket-Key Key value pair , Values must be randomly generated 16 byte , With base64 code
7. If the client is a browser , Then the request header must contain a Origin Key value pair
8. The request header must contain a Sec-WebSocket-Version Key value pair , Value must be 13
9. The request header can contain a Sec-WebSocket-Protocol Key value pair
10. The request header can contain a Sec-WebSocket-Extensions Key value pair
Let's take a look ESP32 Sent for handshake GET Request package , Basically meet the requirements of the handshake request package in the Protocol .
If the server chooses to receive the connection , It must reply to a valid HTTP Response package .
stay WebSocket Reply to the server in the agreement document HTTP Response There are the following requirements :
- The status code must be
101, The reason phrase can beSwitching Protocols - There must be... In the first line
Upgradekey , The value iswebsocket - There must be... In the first line
Connectionkey , The value isUpgrade - There must be... In the first line
Sec-WebSocket-Acceptkey , The value is based on the client Get Request MediumSec-WebSocket-KeyKey value generation , The process goes through SHA-1 Encrypted into 20 Bytes of data , After taking these data as base64 code .
Let's take a look wscat reply HTTP Response package , Basically meet the requirements in the agreement .
Data interaction
Data communication between client and server , The first thing to understand is WebSocket Data format in .WebSocket The data frame in is relatively simple , The format is as follows :
FIN: 1 bitUsed to indicate whether this frame is the last frame of the whole message to be sent . If the whole message can be sent in one frame , Then also set .
RSV1,RSV2,RSV3: 1 bitRetain . It has to be for 0.
opcode: 4 bitsopcode . It's right
Payload DataExplanation of domain . If the receiver receives an undefined opcode , Must end WebSocket Connect .value Definition %x0 It indicates that this frame data is a continuation of the previous frame data , This frame is a continuous frame %x1 It indicates that this frame data is a text frame %x2 It indicates that this frame data is a binary data frame %x3 - 7 Retain , Applicable to future non control frames %x8 Indicates that this frame is a disconnected frame %x9 ping frame %xA pong frame %xB - F Retain , Suitable for future control frames MASK: 1 bitMask bit . To specify
Payload DataWhether the data in the domain passesXOR( Exclusive or ) operation . If the value is1, beMasking-keyThe value in the field is used to restore the processXOR( Exclusive or ) Data from the past . All frames sent from the client to the server must be set1.Payload len: 7 bits, 7 + 16 bits, 7 + 64 bitsLoad length bit , Must be combined with
Extended payload lengthFields together indicatePayload DataLength of data in the field , With byte In units of . Be carefulPayload lenDomain andExtended payload lengthAll domains adopt Network byte order ( Big end model ).- When
Payload lenThe value in the field is [0, 125] In the middle of the day , The value in this field represents the actual data length , hereExtended payload lengthThe domain disappears .Payload lenThe field is followed byMasking-keyDomain . - When
Payload lenThe value in the field is 126 when , The value in this field indicates the subsequentExtended payload lengthDomain is 16 bits.Payload DataThe length of data in the field is determined byExtended payload lengthThe domain specifies . - When
Payload lenThe value in the field is 127 when , The value in this field indicates the subsequentExtended payload lengthDomain is 64 bits.Payload DataThe length of data in the field is determined byExtended payload lengthThe domain specifies .
- When
Masking-key: 0 bit or 32 bitsMask key bit .
MASKDomain is1Effective when . All frames sent from the client to the serverMASKThe domain must be set to1. let me put it another wayMasking-keyDomain is specially used to serve the server . The server needs key To decodePayload lenData in domain . The frame settings sent by all servers to clientsMASKDomain is0, In this frameMasking-keyDomain ellipsis .
To be continued …
边栏推荐
- 正则表达式的应用及分组
- Game pause
- 树和二叉树(C语言)
- Swap parity bits in binary bits
- Hefei University of technology information theory and coding course design, including code, visual interface, course design report
- The heavy performance of karma in the four training of Li Fan
- Unity camera picture making panorama | screenshot making panorama
- Some topics such as ctfhub web information disclosure WP CSDN creation punch in
- Code random notes_ Array_ 27 remove elements
- How to use fmetp steam V2 (I)
猜你喜欢

顶点缓冲区与着色器 (The Cherno + LeranOpenGL)笔记

Ctfshow VIP limited and free topics CSDN creative punch in

VRTK功能教学(一):手柄传送丨手柄瞬移丨触摸板瞬移丨传送

My redis collation

Introduction to C language arrays, functions, operators, and keywords

Freshman summer internship Day5_ three

Vs2019 project packaging exe tutorial

How to use C language to realize headless one-way acyclic list single list?

Greatest common divisor and least common multiple

大一暑假实习day7总结
随机推荐
MRS +Apache Zeppelin,让数据分析更便捷
New attributes of ES6
MQ Getting Started tutorial
Libcurl redirection
交换二进制位中的奇偶位
RPC-BDY(1)-一个最简单RPC实现
Programming Xiaobai's first blog
Greatest common divisor and least common multiple
抽象类
Leetcode-384. clutter array
Freshman summer internship Day5_ one
OpenGL新建一个窗口
JDBC quick start
C language implementation of three chess
The heavy performance of karma in the four training of Li Fan
Leetcode-528. random selection by weight
Unity3d audio system aduio sound music control audio playback audio pause audio control
我的第一篇博客 | 记录编程之路的初心与目标
大一暑假实习day6_拼图游戏
DOM - node operation