Original link: https://5ime.cn/ws-notify.html
WebSocket is a protocol for full-duplex communication over a single TCP connection, allowing the server to actively push data to the client.
Recently, I was developing the公告实时推送
in MoeCTF
. I originally wanted to implement it through轮询
, but after thinking about轮询
, the client actively sends requests to the server. If a single user feels okay, but if there are too many online users If the request is made in a轮询
manner, it will not only cause a large number of useless requests to cause excessive load.
So I plan to use WebSocket
to make a long connection and reduce requests. Since there are not too many requirements for WebSocket
, the WorkerMan
developed based on GatewayWorker
is directly selected.
Install
ThinkPHP5
can only install version 2.*
1 |
|
Events
Modify the onConnect
and onMessage
methods in vendor/topthink/think-worker/src/Events.php
1 |
|
run
Note that you need to restart every time you modify the configuration of WorkerMan
1 |
|
controller
When the server receives a connection request, it determines whether the user is logged in. If already logged in, add the client_id
to the group All
1 |
|
User Interface
1 |
|
Effect
The data requested by the administrator through the push
method will be pushed to all logged in users.
This article is reprinted from: https://5ime.cn/ws-notify.html
This site is for inclusion only, and the copyright belongs to the original author.