尊龙凯时人生就是博

SwooleÐÔÄÜÍ»ÆÆ£ºÒì²½tcp server¿ª·¢Ö¸ÄÏ

swooleÊÇphpÓïÑÔϵÄÒ»¸öÒì²½¡¢²¢ÐС¢¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬Äܹ»ÊµÏÖÒì²½tcp/udp¡¢Òì²½mysqlµÈ¸ßÐÔÄÜÍøÂçÓ¦Óá£Ïà±È´¿´âµÄphpÔÚÍøÂçͨѶÉϵÄȱÏÝ£¬swooleÄܹ»´ó´óÌá¸ßÍøÂçÓ¦ÓõÄÐÔÄÜ£¬²¢ÇÒïÔ̭ЧÀÍÆ÷´ø¿íºÍcpuµÄʹÓã¬ÊÇÒ»¸öºÜÊÇÊÊÓõŤ¾ß¡£

±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSwoole¿ò¼Ü¾ÙÐÐTCPЧÀ͵Ŀª·¢¡£ÔÚ±¾ÎÄÖÐÎÒÃǽ«Í¨¹ý̽Ë÷SwooleµÄ¿ò¼Ü¡¢APIºÍʾÀýÀ´ÏàʶÔõÑù¹¹½¨¸ßЧ¡¢¿ÉÀ©Õ¹µÄÒì²½TCPЧÀÍÆ÷¡£

µÚÒ»²½£º×°ÖÃSwoole

ÔÚÓëSwoole¿ª·¢ÏàͬµÄPHPÇéÐÎÏ£¬Í¨¹ýcomposer×°ÖÃ×îаæµÄSwoole£º

composer require swoole/swoole

µÇ¼ºó¸´ÖÆ

ͬÑù¿ÉÒÔͨ¹ýÔ´Âë×°ÖÃSwoole£¬¿ÉÒÔ´ÓGitHubÏÂÔØÔ´Âë²¢±àÒ룬ȻºóʹÓÃPHPÀ©Õ¹¡£

µÚ¶þ²½£º½¨ÉèTCPЧÀÍÆ÷

ʹÓÃSwoole¿ò¼Ü½¨ÉèTCPЧÀÍÆ÷ºÜÊÇÈÝÒס£Í¨¹ýÏÂÃæµÄ´úÂ룬¿ÉÒÔ½¨ÉèÒ»Ö»¼òÆÓµÄEcho TCPЧÀÍÆ÷£º

$server = new SwooleServer('127.0.0.1', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->on('connect', function ($server, $fd){
    echo "Client {$fd} connected.
";
});

$server->on('receive', function ($server, $fd, $reactor_id, $data){
    $server->send($fd, "Server: " . $data);
});

$server->on('close', function ($server, $fd){
    echo "Client {$fd} disconnected.
";
});

$server->start();

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæ´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öTCPЧÀÍÆ÷²¢Í¨¹ý$server->onÒªÁì×¢²áÁËÊÂÎñ»Øµ÷º¯Êý¡£connectÊÂÎñÔÚ¿Í»§¶ËÅþÁ¬µ½Ð§ÀÍÆ÷ʱ´¥·¢£»receiveÊÂÎñÔÚÎüÊÕµ½¿Í»§¶ËÊý¾Ýʱ´¥·¢£»closeÊÂÎñÔÚÓë¿Í»§¶Ë¶Ï¿ªÅþÁ¬Ê±´¥·¢¡£

µ±ÎüÊÕµ½¿Í»§¶ËµÄÊý¾Ýʱ£¬ÎÒÃÇͨ¹ý$server->send()ÒªÁì·¢ËÍ»ØÀ´¡£

µÚÈý²½£ºÒì²½±à³Ì

SwooleÒÔÒì²½IOÄ£×Ó×÷Ϊ½¹µã£¬ÖÜÈ«Ö§³ÖÒì²½±à³Ì¡£SwooleÌṩÁËÒ»×é±à³Ì·½·¨ÓëͨÀý±à³ÌÄ£×Ó²î±ðµÄAPI£¬Ê¹PHP¿ª·¢Ö°Ô±Äܹ»ÇáËɵؾÙÐÐÒì²½±à³Ì¡£

ÔÚSwooleÖУ¬Í¬²½µÄPHPº¯Êý±»¸ÄΪÒì²½º¯Êý¡£ÀýÈçfile_get_contents±»¸ÄΪswoole_async_readfile£¬mysql_connect±»¸ÄΪswoole_mysql_connect¡£

ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÒì²½Îļþ¶ÁȡʾÀý£º

$filename = "/tmp/test.txt";
$swoole_event = new SwooleEvent();

$swoole_event->add($fp = fopen($filename, "r"), function($fp){
    echo fread($fp, 8192);
    swoole_event_del($fp);
    fclose($fp);
});

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃSwooleµÄSwooleEventÀàºÍadd()ÒªÁìÒì²½¶ÁÈ¡Îļþ¡£ÎÒÃÇת´ïÒ»¸öÎļþÖ¸ÕëºÍÒ»¸ö»Øµ÷º¯Êý¡£µ±¶ÁÈ¡Íê³Éºó£¬»Øµ÷º¯Êý»á±»Ö´ÐУ¬²¢ÇÒÎļþÖ¸Õë»á±»´ÓÊÂÎñ¼àÌýÆ÷ÖÐɾ³ý¡£

µÚËIJ½£ºÐÔÄÜÌåÑé

SwooleµÄÒì²½TCPЧÀÍÆ÷Äܹ»´¦Àí´ó×ڵIJ¢·¢ÇëÇó£¬ÔÚЧÀÍÆ÷¸ºÔØá¯ÁëÆÚÒ²Äܹ»¼á³Ö¸ßЧµÄÐÔÄÜ¡£ÕâÒâζ×Å£¬ÔÚͬÑùµÄÓ²¼þÌõ¼þÏ£¬Ê¹ÓÃSwoole¿ÉÒÔ»ñµÃ¸ü¸ßµÄÍÌÍÂÁ¿ºÍ¸üµÍµÄÑÓ³Ù¡£

ÒÔÏ´úÂë¿ÉÒÔ¾ÙÐÐÐÔÄܲâÊÔ£º

<?php
$server = new SwooleServer('127.0.0.1', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->set(array(
    'worker_num' => 4,
    'backlog' => 128,
));

$server->on('connect', function ($server, $fd){
});

$server->on('receive', function ($server, $fd, $reactor_id, $data){
    $server->send($fd, "Server: " . $data);
});

$server->on('close', function ($server, $fd){
});

$server->start();

µÇ¼ºó¸´ÖÆ

ÎÒÃÇ¿ÉÒÔʹÓÃabÏÂÁî¾ÙÐвâÊÔ£º

$ ab -c 100 -n 10000 http://127.0.0.1:9501/

µÇ¼ºó¸´ÖÆ

ÔÚ²âÊÔÀú³ÌÖУ¬»úеµÄCPUʹÓÃÂʺÍI/OÆÚ´ýʱ¼ä¶¼»áÏÔ×ÅïÔÌ­£¬·ºÆ𱨸æ¡£

SwooleÊÇÒ»¸öºÜÊÇÊÊÓõĿò¼Ü£¬ËüÌṩÁËÇ¿Ê¢µÄÒì²½IOÖ§³ÖºÍ¸ßÐÔÄÜÍøÂç±à³ÌÄÜÁ¦¡£Ê¹ÓÃSwoole£¬ÔÚͬÑùµÄÓ²¼þÉÏ£¬ÎÒÃÇ¿ÉÒÔ»ñµÃ¸ü¸ßµÄÍÌÍÂÁ¿£¬¸üµÍµÄÑӳٺ͸üÉÙµÄCPUÕ¼Óá£Ô½À´Ô½¶àµÄPHP¿ª·¢Ö°Ô±ÒѾ­×îÏȽÓÄÉSwooleÀ´¹¹½¨¸ßЧ¡¢¿ÉÀ©Õ¹µÄÒì²½ÍøÂçÓ¦Óá£

ÒÔÉϾÍÊÇSwooleÐÔÄÜÍ»ÆÆ£ºÒì²½tcp server¿ª·¢Ö¸ÄϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ