-
Foreword
The difference and meaning of Restful and WebSocket are not difficult to understand logically, but how to use WebSocket is always confused, restfule is very intuitive to understand:
df = restful_get_data result = deal_data(df)
But websocket is pushed data, the data is not in blocks, how to use it?
-
websocket concept
-
Several common concepts
- Handshake : The handshake is the process of ensuring that the server and client are synchronized;
- websocket (Web socket): Web socket is defined as two-way communication between server and client, realizing real-time communication between client and server;
- URL : HTTP has its own mode, WebSocket also has its own mode:
- Full duplex : The communication from both ends has a fairly fast speed.
Before websocket , all communication between the web client and the server only relied on HTTP.
websocket is a TCP- based protocol; a transport layer.
WebSocket is a stateful protocol, and HTTP is a stateless protocol.
- The steps to establish a Web Socket connection are as follows :
- The client establishes a connection through a process called Web Socket handshake;
- The process starts with the client sending a regular HTTP request to the server ;
- Request to upgrade the header . In this request, it informs the server that the request is for a Web Socket connection;
- Web Socket URL uses ws scheme;
A simple example of the initial request header is as follows:
GET ws://websocket.example.com/ HTTP/1.1 Origin: http://example.com Connection: Upgrade Host: websocket.example.com Upgrade: websocket
-
websocket event
The four main Web Socket API events, each event by implementing respectively
onopen
,onmessage
,onclose
,onerror
and other functions to handle:-
turn on
When a connection is established between the client and the server, an
open
event is triggered from the Web Socket instance . It is called the initial handshake between the client and the server. The event that is triggered when the connection is established is calledonopen
.onopen
Refers to the initial handshake between the client and server. -
News
A message event usually occurs when the server sends some data. The messages sent by the server to the client can include plain text messages, binary data or images. Whenever data is sent, the
onmessage
function is triggered .This event acts as the client's ear to the server, and the
onmessage
event is triggered whenever the server sends data . -
shut down
The shutdown event marks the end of communication between the server and the client. With
onclose
the help of the event, the connection can be closed. After theonclose
communication is marked with the help of the event, the server and the client cannot transmit messages further. The shutdown event may also occur due to poor connection. -
error
Some erroneous error flags occur during communication. It was
onerror
marked with the help of an incident. After an error, the connection is always terminated.Event implementation effect onLoad Create an object to initialize the connection onOpen Establish connection with server and send status onMessage Display information onSend Send a message to the server
-
-
websocket operation
Two main operations are supported:
-
websocket life cycle
-
websocket与restful
Websocket and restful exist side by side, but carefully distinguished, restful is an API based on http protocol . In fact, websocket directly corresponds to http . Both websocket and http are protocols, so there will be a conceptual correspondence deviation.
In restful , all HTTP communication is guided by the client.
-
websocket与websockets
You can see these two packages in the code:
import websocket # 是一个websocket服务的client端模块,websocket-client import websockets # 同时包含server端和client端
As far as subscription data is concerned, first use websocket .
-
websocket-client(websocket)
There is a basic usage introduction on github .
-
websockets ( official website EBEY tutorial )
See the eBay tutorial.
-
References
Understand the logical ecology of Python using WebSocket
Guess you like
Origin blog.csdn.net/The_Time_Runner/article/details/105646308
Recommended
Ranking