PHP code example of lifetime / swoole-server

1. Go to this page and download the library: Download lifetime/swoole-server library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lifetime / swoole-server example snippets


$config = new \swoole\Config([
    'host' => '0.0.0.0',
    'port' => 9501,
    'mode' => Options::MODE_PROCESS,
    'sock_type' => Options::SOCK_TYPE_TCP,
]);
$config->setConfig([
  'worker_num' => 1
]);

$config = new \swoole\Config();
$config->setHost('0.0.0.0')
    ->setPort(9501)
    ->setMode(Options::MODE_PROCESS);



class Config extends \swoole\Config
{
    protected $host = '0.0.0.0';
    protected $port = 9501;
    protected $reactorNum = 1;
    protected $workerNum = 4;
}



use swoole\event\Mqtt;

class MqttEvent extends Mqtt
{
    public static function onMqttConnect($server, int $fd, int $reactorId, array $data)
    {
        // 验证客户端信息

        //发送确认包
        $server->send($fd, self::encode([
            'cmd' => MQTT::CONNACK, // CONNACK固定值为2
            'code' => 0, // 连接返回码 0表示连接已被服务端接受
            'session_present' => 0
        ]));
    }
}



// 创建配置类
$config = new \swoole\Config();
// 设置事件类
$config->setEventClass('TcpUdpEvent');
// 初始化并启动服务器
\swoole\server\TcpUdp::instance($config)->initServer()->start();


// 创建配置类
$config = new \swoole\Config();
// 设置事件类
$config->setEventClass('TcpUdpEvent');
// 初始化服务器
$server = \swoole\server\TcpUdp::instance($config)->initServer();
/**
 * 用户进程实现了广播功能,循环接收unixSocket的消息,并发给服务器的所有连接
 */
$process = new Swoole\Process(function ($process) use ($server) {
    $server = $server->getServer();
    $socket = $process->exportSocket();
    while (true) {
        $msg = $socket->recv();
        foreach ($server->connections as $conn) {
            $server->send($conn, $msg);
        }
    }
}, false, 2, 1);
// 添加一个用户自定义的工作进程
$server->getServer()->addProcess($process);
// 启动服务器
$server->start();

function stop()
{
  // 创建配置类
  $config = new \swoole\Config();
  $server = \swoole\server\TcpUdp::instance($config);
  $masterPid = $server->getMasterPid();
  if (!$server->isRunimg($masterPid)) {
    echo "没有正在运行的服务器\n";
    return;
  }
  \Swoole\Process::kill($master, SIGTERM);
  echo "操作成功\n";
}


$config = new \swoole\Config();
$config->setFileMoniotr(true);
$config->setFileList([
    __DIR__ . '/func.php',
]);


    $config = [
        'port' => 9503, // 端口
        'task_worker_num' => 1, // 任务工作进程数
        'pid_file' => '', // PID文件地址
        'task_file_path' => '', // 任务列表文件地址
        'log_path' => '', // 日志文件地址
        'daemonize' => false, // 以守护进程的方式运行
        'hit_update' => true, // 热更新
    ];
>


namespace timer;

use swoole\extend\abstracts\Timer;

class Test extends Timer
{
    /**
     * 定时任务 秒 分 时 天 月 周
     * @var string
     */
    public $crontab = "*/3 * * * * *";
    /**
     * 是否启用
     * @var boolean
     */
    public $enable = false;

    public function handle()
    {
        // 业务
    }
}
>


swoole\extend\server\Timer::instance([
    'task_file_path' => __DIR__ . "/crontab.php",
    'log_path' => __DIR__ . "/log/timer/",
])->start();
>