Swoole³£Óù¤¾ßDemonsterÏÈÈÝÓëʹÓÃÒªÁì
swoole³£Óù¤¾ßdemonsterÏÈÈÝÓëʹÓÃÒªÁì
SwooleÊÇÒ»¿î³£ÓõĸßÐÔÄÜÍøÂç¿ò¼Ü£¬ËüµÄÒì²½¶àÀú³ÌÄ£×ÓºÍÇ¿Ê¢µÄÍøÂç±à³Ì¹¦Ð§£¬Ê¹µÃËüÔÚWebЧÀÍ¡¢ÓÎϷЧÀ͵ÈÁìÓòÓÐ×ÅÆÕ±éµÄÓ¦Óá£È»¶ø£¬¿ª·¢Ö°Ô±ÔÚʹÓÃSwooleÀú³ÌÖг£³£»áÓöµ½Ò»Ð©†ªàµÄÎÊÌ⣬ÀýÈ磺ÔõÑùÅŲéSwooleÀú³Ì¹ÊÕÏ¡¢ÔõÑùÆÊÎöSwooleÀú³ÌÄÚ´æÕ¼ÓÃÇéÐεȵȡ£ÕâЩÎÊÌâ¹ØÓÚSwooleµÄ¸ßЧÔËÐÐÀ´ËµºÜÊÇÖ÷Òª¡£Òò´Ë£¬±¾ÎĽ«Ïò¸÷ÈËÏÈÈÝÒ»¿îSwoole³£Óù¤¾ß¡ª¡ªDemonster¡£
Ò»¡¢Ê²Ã´ÊÇDemonster£¿
DemonsterÊÇÒ»¿î¿ªÔ´µÄSwoole³£Óù¤¾ß£¬ËüÄܹ»×ÊÖú¿ª·¢Ö°Ô±ÅŲéSwooleÀú³Ì¹ÊÕÏ¡¢ÆÊÎöSwooleÀú³ÌÄÚ´æÕ¼ÓÃÇéÐΡ¢²¢¼à¿ØSwooleÀú³ÌµÄ״̬µÈµÈ¡£DemonsterÌṩÁËһϵÁÐÏÂÁîÐй¤¾ßºÍUI½çÃ棬ÉÁ¿ª·¢Ö°Ô±¿ÉÒÔÀû±ãµØ²Ù×÷¡¢Éó²éºÍÆÊÎöSwooleÀú³ÌµÄÖÖÖÖÐÅÏ¢¡£
¶þ¡¢DemonsterµÄ×°ÖÃ
DemonsterµÄ×°ÖúÜÊǼòÆÓ£¬Ö»ÐèҪʹÓÃComposer¼´¿É¡£°ì·¨ÈçÏ£º
1.ÔÚÏîÄ¿¸ùĿ¼Ï½¨Éècomposer.jsonÎļþ£¬Ìí¼ÓÒÔÏÂÄÚÈÝ£º
{ "require": { "yueliangdali/demonster": "dev-master" } }
µÇ¼ºó¸´ÖÆ
2.ÔÚÏÂÁîÐÐÖÐÔËÐÐcomposer updateÏÂÁîÏÂÔز¢×°ÖÃÐëÒªµÄ¿âºÍÒÀÀµ¡£
3.ÔÚnginx»òapacheµÈWebЧÀÍÆ÷ÉÏÉèÖÃÒ»¸öÐéÄâÖ÷»ú£¬²¢½«¸ùĿ¼ָÏòDemonsterÏîÄ¿µÄpublicĿ¼¡£ÀýÈ磬ÔÚnginxЧÀÍÆ÷ÉϵÄÉèÖãº
server { listen 80; server_name demo.demonster.com; root /var/www/demonster/public; location / { index index.php; if (!-e $request_filename){ rewrite ^/(.*)$ /index.php?$1 last; break; } } location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/demonster/public$fastcgi_script_name; include fastcgi_params; } }
µÇ¼ºó¸´ÖÆ
4.ÔÚSwooleÏîÄ¿ÖÐÒýÈëDemonsterµÄÃüÃû¿Õ¼äºÍÆô¶¯À༴¿É×îÏÈʹÓá£ÀýÈ磬ÔÚÒ»¸öWebSocketЧÀÍÆ÷ÖУº
<?php require_once __DIR__ . '/vendor/autoload.php';//¼ÓÔØComposer×Ô¶¯ÌìÉúµÄautoload.phpÎļþ use yueliangdalidemonsterDemonster; use SwooleWebSocketServer; $server = new Server("127.0.0.1", 9501); $server->set([ 'worker_num' => 2, 'task_worker_num' => 2, 'daemonize' => false, ]); Demonster::start();//Æô¶¯Demonster $server->on('open', function (Server $server, $request) { echo "server: handshake success with fd{$request->fd} "; $server->task('hello, task');//´¥·¢Ò»¸ötaskʹÃü }); $server->on('message', function (Server $server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish} "; $server->push($frame->fd, "this is server"); }); $server->on('close', function ($ser, $fd) { echo "client {$fd} closed "; }); $server->on('task', function ($server, $task_id, $worker_id, $data) { echo "New AsyncTask[id=$task_id]" . PHP_EOL; $server->finish("$data -> OK"); }); $server->on('finish', function ($server, $task_id, $data) { echo "AsyncTask[$task_id] Finish: $data" . PHP_EOL; }); $server->start();
µÇ¼ºó¸´ÖÆ
Èý¡¢DemonsterµÄ¹¦Ð§
1.չʾЧÀÍÆ÷µÄ״̬
DemonsterÌṩÏÂÁîdemonster status£¬¿ÉÒÔÏÔʾЧÀÍÆ÷µÄ״̬£¬°üÀ¨£ºSwooleЧÀÍÆ÷µÄËÀÑ»·ÊÇ·ñÕýÔÚÖ´ÐС¢ÒÑÆô¶¯µÄWorkerÀú³ÌºÍTaskÀú³ÌÊýÄ¿¡¢TCPºÍUDPÅþÁ¬ÊýµÈµÈ¡£ÀýÈ磺
$ demonster status _____ ____ / ____| / __ | (___ ___ ___ _| | | |_ __ ___ / _ / _ '__| | | | '__| ____) | __/ __/ | | |__| | | |_____/ ___|___|_| ____/|_| Swoole Server Status Swoole version:4.4.17 php version:7.2.24 PHP memory:1.37MB ----------------------------------------- start at:2021-07-05 12:50:12 swoole version:4.4.17 (reactor_num:2, worker_num:2, task_worker_num:2) listen:tcp://0.0.0.0:9501 ---Processes--- master pid: 23291 status: start memory: 1MB ©À©¤worker pid: 23295 status: start memory: 2MB ©À©¤worker pid: 23296 status: start memory: 2MB ©À©¤task pid: 23297 status: start memory: 2MB ©À©¤task pid: 23298 status: start memory: 2MB ©¸©¤manager pid: 23294 status: start memory: 2MB ---Connect Info--- TCP LiveConnections: 8 Not ESTABConnections: 0 SWNotsTCP TimeWaitConnections: 0 UDPLiveConnections: 0 Lastreloadtime: 2021-07-06 10:35:38 reload: DYNAMIC
µÇ¼ºó¸´ÖÆ
2.ÆÊÎöЧÀÍÆ÷ÄÚ´æʹÓÃÇéÐÎ
DemonsterÌṩÏÂÁîdemonster memory£¬¿ÉÒÔÏÔʾSwooleЧÀÍÆ÷µÄÄÚ´æÇéÐΣ¬°üÀ¨£ºÀú³ÌÊýÄ¿¡¢PID¡¢ÄÚ´æÕ¼ÓÃÇéÐεȵȡ£¸ÃÏÂÁ¿ÉÒÔת´ïÒ»¸ö-c²ÎÊý£¬ÓÃÓÚ¶ÔËùÓÐÀú³ÌµÄÄÚ´æÕ¼ÓÃÇéÐξÙÐÐͳ¼ÆºÍÅÅÐò¡£ÀýÈ磺
$ demonster memory _____ ____ / ____| / __ | (___ ___ ___ _| | | |_ __ ___ / _ / _ '__| | | | '__| ____) | __/ __/ | | |__| | | |_____/ ___|___|_| ____/|_| Swoole Server Memory Usage Swoole version:4.4.17 php version:7.2.24 PHP memory:1.37MB ------------------------------- Swoole worker process mask memory usage PID POW Used(M) RealUsed(M) Diff(M) 23295 22 3.860 1.426 2.434 23296 22 3.860 1.427 2.433 ------------------------------- Swoole task process mask memory usage PID POW Used(M) RealUsed(M) Diff(M) 23297 22 3.860 1.926 1.934 23298 22 3.860 1.925 1.935 ------------------------------- Swoole manager process mask memory usage PID Used(M) % RealUsed(M) % Diff(M) 23294 1.038M 0.06% 1.124M 0.07% 85.76KB ------------------------------- Total Memory Usage Used(M) RealUsed(M) Total 33MB 13.77MB Avg(each) 3.7MB 1.53MB
µÇ¼ºó¸´ÖÆ
3.Demonster UI½çÃæ
³ýÁËÏÂÁîÐй¤¾ßÍ⣬Demonster»¹ÌṩÁËÒ»¸öUI½çÃ棬Àû±ã¿ª·¢Ö°Ô±ÔÚWeb¶Ë¶ÔSwooleЧÀÍÆ÷¾ÙÐмà¿ØºÍÖÎÀí¡£
UI½çÃæµÄ»á¿´·¨Ö·Îª£ºhttp://yourhost.com/demonster/index.php£¨ÐèҪƾ֤ÏÖʵµÄWebЧÀÍÆ÷ÉèÖþÙÐÐÐ޸ģ©¡£·¿ªºó¿ÉÒÔ¿´µ½Ò»¸ö¼òÆÓµÄÒ³Ã棬ÆäÖÐÏÔʾÁËSwooleЧÀÍÆ÷µÄ״̬ºÍÀú³ÌÐÅÏ¢£¬Í¬Ê±ÌṩÁËÀú³Ì¼à¿ØºÍCPU¡¢ÄÚ´æÇéÐÎÆÊÎöµÈ¹¦Ð§¡£
ËÄ¡¢Ð¡½á
DemonsterÊÇÒ»¿îºÜÊÇÊÊÓõÄSwoole³£Óù¤¾ß£¬ËüÌṩÁ˸»ºñµÄ¹¦Ð§ºÍ¼òÆÓÒ×ÓõIJÙ×÷½çÃ棬Àû±ãÁË¿ª·¢Ö°Ô±ÔÚSwoole¿ª·¢Àú³ÌÖеÄÅÅ´íºÍ¼à¿ØÊÂÇ顣ͬʱ£¬DemonsterÊÇÒ»¸ö¿ªÔ´¡¢¸ß¿É¿¿µÄ¹¤¾ß£¬ÖµµÃ¿ª·¢Ö°Ô±ÔÚSwoole¿ª·¢ÖмÓÒÔʵÑéºÍʹÓá£
ÒÔÉϾÍÊÇSwoole³£Óù¤¾ßDemonsterÏÈÈÝÓëʹÓÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡