Home > Article > Backend Development > How does swoole implement a single query and then send it to everyone
My requirement is to query the database every once in a while and then push it to everyone
The code is similar to the following:
while(true){
<code>$sql ="select 1+1"; $count=$mysql->query($sql); foreach($serv->connections as $fd) { $serv->push($fd, $count); } sleep(1000);</code>
}
If I now use swoole websocket's on('message') to write this, it will open a copy of this code and run it every time the client connects.
How can we run a code so that all customers can receive it? .
My requirement is to query the database every once in a while and then push it to everyone
The code is similar to the following:
while(true){
<code>$sql ="select 1+1"; $count=$mysql->query($sql); foreach($serv->connections as $fd) { $serv->push($fd, $count); } sleep(1000);</code>
}
If I use swoole websocket's on('message') to write this, it will open a copy of this code and run it every time the client connects.
How can we run a code so that all customers can receive it? .
I remember there is a timer, right? But I don’t understand either
If you only use messages, divide the messages into two types, one is the message sent by the administrator, and the other is sent to other users. Then every time you receive a message from an administrator, check the database once, and then send it to other users in bulk. people.
If you think this will take up too much time, you can put the main tasks in tasks, so that the administrator can return immediately after sending a message to process other messages, and leave complex things to tasks.