Home  >  Article  >  Backend Development  >  How does swoole implement a single query and then send it to everyone

How does swoole implement a single query and then send it to everyone

WBOY
WBOYOriginal
2016-08-18 09:16:201287browse

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? .

Reply content:

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn