×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃThinkPHP6¾ÙÐÐ׼ʱʹÃü²Ù×÷ £¿

thinkphp6ÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶à±ãµ±µÄ¹¦Ð§À´×ÊÖúÎÒÃÇ¿ª·¢¸ßЧµÄwebÓ¦ÓóÌÐò¡£ÆäÖÐÖ®Ò»¾ÍÊÇ׼ʱʹÃü¡£ÔÚ±¾ÎÄÖУ¬ÎÒ½«ÏÈÈÝÔõÑùʹÓÃthinkphp6µÄ׼ʱʹÃü¹¦Ð§À´Ö´ÐÐһЩ°´ÆÚÐèÒªÖ´ÐеÄʹÃü£¬ÈçÊý¾Ý±¸·Ý¡¢·¢ËÍÓʼþµÈ¡£

×°ÖÃ

ThinkPHP6 ʹÓÃÁË Swoole µÄЭ³Ì׼ʱÆ÷£¬´Ó¶øʵÏÖÁ˸ßЧµÄ׼ʱʹÃü¹¦Ð§¡£ÔÚ×îÏÈʹÓÃ׼ʱʹÃüÇ°£¬ÄãÐèҪȷ±£ÄãÒѾ­×°ÖÃÁË Swoole À©Õ¹¡£

ÔÚ Composer ÖУ¬Äã¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´×°Öà Swoole£º

composer require swoole/swoole

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê±ÏÖ®ºó£¬ÄãÐèÒªÔÚ config/app.php ÎļþÖÐÆôÓà Swoole À©Õ¹¡£ÕÒµ½ providers Êý×é²¢¼ÓÈë thinkworkerProvider::class£º

'providers' => [
    //¡­
    thinkworkerProvider::class,
],

µÇ¼ºó¸´ÖÆ

½ç˵ʹÃü

ÔÚ ThinkPHP6 ÖУ¬Äã¿ÉÒÔʹÓÃÁ½ÖÖ·½·¨À´½ç˵׼ʱʹÃü¡£Ò»ÖÖÊÇͨ¹ý½¨ÉèÃûΪ appcommand µÄÏÂÁîÀ࣬ÁíÒ»ÖÖÊÇͨ¹ýÀ©Õ¹ thinkworkerServer Àà¡£ÕâÀïÎÒÃǽÓÄɺóÕß¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

ÔÚÄãµÄÏîÄ¿ÖУ¬ÄãÐèÒª½¨ÉèÒ»¸ö¼ÌÐø×Ô thinkworkerServer µÄÀ࣬ÈçÏÂËùʾ£º

<?php
namespace appsocket;

use SwooleCoroutine;
use thinkworkerServer;

class Socket extends Server
{
    protected $socket;

    public function onWorkerStart()
    {
        // ´Ë´¦¿ÉÒÔ½ç˵ÔÚ Worker Àú³ÌÆô¶¯Ê±ÐèÒªÖ´ÐеIJÙ×÷
    }

    public function onConnect($server, $fd)
    {
        // ´Ë´¦½ç˵¿Í»§¶ËÅþÁ¬Ê±Ö´ÐеIJÙ×÷
    }

    public function onReceive($server, $fd, $reactor_id, $data)
    {
        // ´Ë´¦½ç˵¿Í»§¶Ë·¢ËÍÊý¾ÝʱµÄ²Ù×÷
    }

    public function onClose($server, $fd)
    {
        // ´Ë´¦½ç˵¿Í»§¶Ë¶Ï¿ªÅþÁ¬Ê±µÄ²Ù×÷
    }

    public function onTask($server, $task_id, $src_worker_id, $data)
    {
        // ×Ô½ç˵ʹÃü´¦ÀíÒªÁì
    }

    public function onFinish($server, $task_id, $data)
    {
        // ʹÃü¿¢ÊÂʱµÄ²Ù×÷
    }

    public function onPipeMessage($server, $from_worker_id, $message)
    {
        // ´Ë´¦½ç˵Àú³Ì¼äͨѶµÄÂß¼­
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽç˵ÁË onWorkerStart()¡¢onConnect()¡¢onReceive()¡¢onClose()¡¢onTask()¡¢onFinish()¡¢onPipeMessage() µÈÒªÁì¡£ÕâЩҪÁ콫ÔÚ¶ÔÓ¦µÄÊÂÎñ´¥·¢Ê±×Ô¶¯Ö´ÐС£

Ö´ÐÐʹÃü

µ±ÎÒÃÇÏëÔÚºǫ́ʹÃüÖÐÖ´ÐÐij¸öÒªÁìʱ£¬ÎÒÃÇ¿ÉÒÔÔÚ onWorkerStart() ÖÐʹÓà Swoole µÄ tick() ÒªÁìÀ´É趨׼ʱʹÃüµÄÖ´ÐÐƵÂÊ¡£ÀýÈ磬ÎÒÃÇÒÔÏÂÃæµÄ´úÂëΪÀý£º

public function onWorkerStart()
{
    // ÿÎåÃëÖ´ÐÐÒ»´Î backup() ÒªÁì
    SwooleTimer::tick(5000, function () {
        (new Backup())->backup();
    });
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÉ趨ÁËÿ5ÃëÖ´ÐÐÒ»´Î backup() ÒªÁ졣ͨ¹ýÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔʵÏÖ°´ÆÚ±¸·ÝÊý¾Ý¡¢·¢ËÍÓʼþµÈʹÃü¡£

×Ô½ç˵ʹÃü

ÈôÊÇÄãÏëÒªÖ´ÐÐһЩԽ·¢ÖØ´óµÄʹÃü£¬²¢ÇÒÐèÒªÔÚʹÃüÀú³ÌÖÐʹÓõ½Ò»Ð©³¤Ê±¼äÔËÐеIJÙ×÷£¬Äã¿ÉÒÔʹÓà onTask() ºÍ onFinish() ÒªÁìÀ´´¦ÀíÕâЩʹÃü¡£ÀýÈ磬

public function onTask($server, $task_id, $src_worker_id, $data)
{
    // ´Ë´¦¿ÉÒÔ½ç˵ÐèÒªÔÚʹÃüÖÐÖ´ÐеIJÙ×÷
    $result = (new Task())->execute($data);
    return $result;
}

public function onFinish($server, $task_id, $data)
{
    // ʹÃüÖ´Ðп¢ÊºóµÄ²Ù×÷
    echo "Task #$task_id finished with result: $data
";
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵ÁË onTask() ºÍ onFinish() ÒªÁì¡£ onTask() ÒªÁì½ç˵ÁËÔÚʹÃüÖÐÐèÒªÖ´ÐеIJÙ×÷£¬¶ø onFinish() ·½¹æÔò½ç˵ÁËʹÃü¿¢ÊºóµÄ²Ù×÷¡£ÈôÊÇÐèÒªÔÚʹÃüÖÐÖ´ÐÐһЩ³¤Ê±¼äÔËÐеIJÙ×÷£¬Äã¿ÉÒÔÔÚ onTask() ÒªÁìÖÐʹÓÃЭ³ÌÀ´ÊµÏÖ¡£

×ܽá

ͨ¹ýʹÓà ThinkPHP6 µÄ׼ʱʹÃü¹¦Ð§£¬Äã¿ÉÒÔʵÏÖÐí¶à°´ÆÚÐèÒªÖ´ÐеIJÙ×÷£¬ÀýÈçÊý¾Ý±¸·Ý¡¢·¢ËÍÓʼþµÈʹÃü¡£Äã¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇ󣬽ç˵×Ô¼ºµÄʹÃü£¬ÔÚʹÃüÖ´ÐÐÀú³ÌÖÐÖ´ÐÐһЩ³¤Ê±¼äÔËÐеIJÙ×÷¡£Ï£ÍûÕâƪÎÄÕÂÄܹ»×ÊÖúÄã¸üºÃµØʹÓà ThinkPHP6 µÄ׼ʱʹÃü¹¦Ð§¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐ׼ʱʹÃü²Ù×÷ £¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ