Workerman communicates with hardware, how to send more than 3 packets at regular intervals? Such as packet 1. The interval is 3 seconds, packet 2 is sent, and packet 3 is sent at an interval of 3 seconds.

three material packages should be sent at regular intervals

Jul.07,2021

send with for loop, or directly three calls to send. sleep 3 seconds each time.
it doesn't make much sense for you to do this. You can send it in three seconds locally, but the receiver doesn't have to build it in three seconds. It has something to do with the network.


receive a message and write a timer reply three times


if workerman has a timer, use timer.

use Workerman\Lib\Timer;
use Workerman\Worker;

$worker = new Worker('tcp://0.0.0.0:8181');
$worker->onMessage = function($con, $data){
    $con->send('1');
    Timer::add(3, function()use($con){
        $con->send('2');
    }, null, false);
    Timer::add(6, function()use($con){
        $con->send('3', null, false);
    });
};
Worker::runAll();

cli mode is recommended for scheduled tasks

can be extended with swoole workerman event, etc.

socket itself makes use of event-driven io multiplexing performance such as eventloop. Of course, the interval can be used with relevant timing

.

there are two simple ways

  1. while endless loop

    <?php
    $data = ['1','2','3'];
    $i    = 0;
    while(true){
        sendData($data[i]);
        $iPP;
        if($i>count($data)){
            $i=0;
        }
        sleep(3)
    }
  2. scheduled tasks in seconds can use workman or swoole
Menu