A brief discussion on WebSocket interface testing

2023.10.04

A brief discussion on WebSocket interface testing

WebSocket makes data exchange between the client and the server simpler, allowing the server to actively push data to the client.

What is WebSocket

WebSocket is a protocol based on full-duplex communication on a single TCP connection. It solves the shortcoming of the HTTP protocol that is not suitable for real-time communication. Compared with the HTTP protocol, the WebSocket protocol implements persistent network communication and can realize client and The long connection on the server side is capable of two-way real-time communication, and the protocol name is "ws".

WebSocket makes data exchange between the client and the server simpler, allowing the server to actively push data to the client. In the WebSocket API, the browser and the server only need to complete a handshake, and a persistent connection can be created directly between the two for bidirectional data transmission. In the WebSocket API, the browser and the server only need to perform a handshake action, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two.

HTTP and WebSocket

Features of WebSocket

  • Built on the TCP protocol, server-side implementation is relatively easy.
  • It has good compatibility with HTTP protocol. The default ports are also 80 and 443, and the handshake phase uses the HTTP protocol, so it is not easy to block during the handshake and can pass various HTTP proxy servers.
  • The data format is relatively lightweight, has low performance overhead, and efficient communication.
  • Text can be sent as well as binary data.
  • There is no same-origin restriction and the client can communicate with any server.
  • The protocol identifier is ws (or wss if encrypted) and the server address is the URL.
ws://example.com:80/some/path
  • 1.

 

What is a socket?

Two programs on the network exchange data through a two-way communication connection. One end of this connection is called a Socket. Therefore, at least a pair of port numbers is required to establish a network communication connection.

The essence of Socket: It is an encapsulation of the TCP/IP protocol stack. It provides an interface for TCP or UDP programming, and is not another protocol. Through Socket, you can use the TCP/IP protocol.