Go implements a simple websocket push program

Recently, a websocket push function will be added to a K-line server implemented by go.

The initial idea is to push the corresponding K-line data to each subscription unit immediately after the completion of the per-minute data of each product.

1. Scene abstraction:

1 user connects to the server

2 The user subscribes to a certain cycle of a certain product

3 A user unsubscribes from a certain category for a certain cycle

4 User disconnects from server

subscription:

 Push:

 2. Program framework

Since I am not familiar with the go language, fortunately, someone on the Internet has made a mature framework: GitHub - gorilla/websocket: A fast, well-tested and widely used WebSocket implementation for Go. This solves the problem of how to use websocket

3. Main business logic:

1. Open two threads for each connection

One thread writes data to writePump, and one thread reads data to readPump. Whenever a client connects to the server, these two threads are started immediately.

Read thread logic diagram:

Write thread logic diagram:

2. Open a master control thread func (h *Hub) Run() when the program starts

This thread is used to handle client connection, disconnection, subscription and broadcast corresponding information to clients who subscribe to the information.

 The logic diagram is as follows:

2.1 Processing Registration

The logic of processing registration is very simple, just place the pointer of the client in the pointer map, the code is as follows:

// 注册,客户端连接上来的处理逻辑
func (h *Hub) registerMsg(client *Client) {
	h.clients[client] = true
}

2.2 Handling Logout

Logout needs to be divided into two steps. First, delete the corresponding pointer in the client pointer collection, and delete the corresponding sending channel at the same time. Then iterate through the content subscription collection and delete the elements of the corresponding client pointers. Finally, if it is found that there is no client pointer on a certain subscription content, delete the subscription content together.

The program logic is as follows:

2.3 Subscription processing

The most important part of subscription is the subscription relationship. We can use the contract and period as the key, and use the pointer set connected from the client as the value. Whenever a user subscribes to a certain content, add the corresponding user to the pointer set below the corresponding content. The pointer is enough, and the subscription relationship is as follows:

 

2.4 Push processing

There are two types of push processing, one is to push the data subscribed by users to them, and the other is to push heartbeat packets to users who have no data push or subscription action within 30 seconds.

2.4.1 Push subscription content : When the type of data subscribed by the user arrives, the system checks the user pointer on the subscription, pushes the data to the channel corresponding to the pointer, and each channel pushes it to its own client.

2.4.2 Push heartbeat packet: When a connected client has no subscription request or push data within 30 seconds, the system will automatically push heartbeat packet to keep the connection from being disconnected.

 

Guess you like

Origin blog.csdn.net/luhouxiang/article/details/127296903