NodeJS learning notes on network programming

  • 2020-03-30 03:39:48
  • OfStack

Node provides a rich network programming module

Node The module agreement
net TCP
dgram UDP
http HTTP
https HTTPS

TCP service events fall into the following two categories

(1), Server events

For a server created through net.createserver (), which is an EventEmitter instance, custom events can be:

Listening: triggered after the call to listen() binds the port or Domain Socket, short for server.listen(port, listener), passed in through the second parameter.

Connection: triggered when each client socket connects to the server, succinctly written by net.createserver (), with the last parameter passed.

Close: triggered when the server is closed. After calling server.close(), the server will stop accepting new socket connections, but keep the existing connection, and wait for all connections to break, triggering the event.

Error: this event is triggered when an exception occurs on the server.

(2), Connection event

The server can stay connected to multiple clients at the same time, which is typically a writable and readable Stream object for each connection. The Stream object can be used for communication between the server and the client, either by reading data from one end to the other through the data event, or by sending data from one end to the other through the write() method.

Data: when one side calls write() to send data, the other side triggers a data event, and the data passed by the event is the data sent by write()

End: this event is triggered when either end of the connection sends FIN data.

Connect: this event is used on the client side and is triggered when the socket successfully connects to the server.

Drain: when either end calls write() to send data, the current end fires the event.

Error: when an exception is sent

Close: triggered when the socket is completely closed

Timeout: when the connection is no longer active after a certain amount of time, this event is triggered to notify the user that the connection is idle.

TCP has a certain optimization strategy for small packets in the network: Nagle algorithm, which is triggered when the data reaches a certain amount.

UDP service

UDP is called the user packet protocol and is not a connection-oriented service. UDP in Node is just an EventEmitter instance, not an instance of Stream, with the following custom events:

(1) message: when the UDP socket listens on the network card port and accepts the message, the triggered data is the message Buffer object and a remote address information.

(2) listening: this event is triggered when the UDP socket starts to listen.

(3) close: this event is fired when the close() method is called, and the message event is no longer fired. To trigger the message event again, you need to rebind.

(4) error: it is triggered when an exception occurs. If it is thrown directly without listening, the process will exit.

The HTTP service

The HTTP module in Node is inherited from the TCP server (net module). It can maintain connections with multiple clients. Since it does not create threads for each connection and keeps the memory footprint low, it can achieve high concurrency. The difference between an HTTP service and a TCP service is that after keepalive is opened, a single TCP session can be used for multiple requests and responses. TCP service is served by connection and HTTP service by request. The HTTP module encapsulates the process of connection to request.

The HTTP module abstracts the read and write of the socket used for connection into ServerRequest and ServerResponse objects, corresponding to request and response operations, respectively.

(1) the HTTP request

For a read operation of a TCP connection, the HTTP module encapsulates it as a ServerRequest object. For example, req.method, req.url and req.headers are abstracted into a read-only stream object. If the business logic needs to read the data in the stream, it can only operate after the end of the data stream.

(2) the HTTP response

The HTTP response encapsulates the write operation of the underlying connection and can be thought of as a writable stream object.

Response message header information methods: res.setheader () and res.writeheader (). You can set setHeader many times, but you must call writeHeader to write the connection.

Methods: res.write() and res.end()

(3) HTTP server events

Connection: when a client establishes a TCP connection with a server, a connection event is triggered

Request: after establishing a TCP connection, the HTTP module abstracts the HTTP request and HTTP response from the data stream at the bottom. When the request data is sent to the server, the event is triggered after the HTTP request header is resolved. After res.end(), the TCP connection is available for the next request.

Close: this event is triggered when the server.close method is called to stop receiving new connections and existing connections are disconnected.

CheckContinue: some clients send a request with an Expect: 100-continue header to the server when sending large data, and the service triggers the event.

Connect: triggered when a client initiates a connect request

Upgrade: when a client requests to upgrade the protocol for the connection, it needs to negotiate with the server to bring the Updagrade field into the request header

ClientError: this event is triggered when the connected client sends an error that is passed to the server

(4) HTTP client

The HTTP module provides http.request(options, connect) for constructing the HTTP client.

The HTTP client is similar to the server. In the ClientRequest object, its event is called response. When ClientRequest parses the response message, the response event is triggered as soon as the response header is parsed.

(5) HTTP client events

Response: this event is triggered when the client corresponding to the request event on the server receives a response after the request is issued.

Socket: triggered when the connection established in the underlying connection pool is allocated to the current request object;

Connect: when a client sends a connect request to the server, the client will trigger the event if the server responds with a 200 status code.

Upgrade: when the client server sends an upgrade request, the client will trigger the event if the server responds to the 101 Switching Protocols state.

Continue: the client initiates the Expect: 100-continue header to the server in an attempt to send large data, which is triggered by the server if the server responds to the 100 continue state

WebSocket service

WebSocket first emerged as an important HTML5 feature, with the following advantages over HTTP:

(1) client and server only establish a TCP connection, can use fewer connections

(2) the WebSocket server can push data to the client, which is far more flexible and efficient than the HTTP request response mode

(3) more lightweight protocol header to reduce data transmission

There is no WebSocket library built into Node, but the community's ws modules encapsulate the underlying implementation of WebSocket such as the famous

Related articles: