È«Õ»¹¤³Ìʦ±Ø±¸ÊÖÒÕÖ®SwooleÊÃ÷Èսƪ
Ëæ×ÅÒƶ¯»¥ÁªÍøµÄѸËÙÉú³¤£¬webÓ¦ÓõÄÐÔÄÜÓë¿ÉÀ©Õ¹ÐÔÓú·¢Êܵ½ÖØÊÓ¡£ÎªÁËÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ£¬Ìá¸ß²¢·¢´¦ÀíÄÜÁ¦£¬Ô½À´Ô½¶àµÄÆóÒµºÍ¿ª·¢Õß×îÏÈÑ¡ÔñʹÓÃswoole£¬Ò»¿î»ùÓÚphpÓïÑÔ¿ª·¢µÄ¸ßÐÔÄÜÍøÂç¿ò¼Ü¡£Õë¶ÔÕâÖÖÇ÷ÊÆ£¬×÷Ϊȫջ¹¤³ÌʦµÄÎÒÃÇÐèҪѧϰÕÆÎÕswoole¡£
SwooleÊÇÒ»¸ö¿ªÔ´µÄ¡¢¸ßЧµÄPHPÍøÂç¿ò¼Ü£¬Ëüµ×²ãʹÓÃC++ÓïÑÔʵÏÖ¡£SwooleÌṩÁËÒì²½µÄ¡¢ÊÂÎñÇý¶¯µÄÍøÂç¿â£¬¿ÉÒÔ×ÊÖúÎÒÃǹ¹½¨¸ßÐÔÄÜ¡¢¸ß²¢·¢µÄWebÓ¦ÓóÌÐò¡£»ùÓÚSwoole£¬ÎÒÃÇ¿ÉÒÔÇáËÉʵÏÖЧÀÍÆ÷¡¢¶àÀú³Ì¡¢²¢·¢Ê¹Ãü¡¢Òì²½I/OµÈÖÖÖÖÖØ´óµÄ³¡¾°£¬Ìá¸ßÓ¦ÓóÌÐòµÄÍÌÍÂÁ¿ºÍÐÔÄÜÌåÏÖ¡£
ÔÚ±¾ÆªÎÄÕÂÖУ¬ÎÒÃǽ«Í¨¹ýÏÖʵ°¸ÀýÀ´Õ¹Ê¾SwooleµÄÓ¦Óã¬Í¬Ê±Ì½ÌÖÈ«Õ»¹¤³ÌʦÐèÒªÕÆÎÕµÄSwooleÊÖÒÕ¡£
Ò»¡¢SwooleÓ¦Óó¡¾°
SwooleÊÊÓÃÓÚÖÖÖָ߲¢·¢¡¢´óÁ÷Á¿µÄÓ¦ÓóÌÐò£¬È绥ÁªÍøÖ±²¥¡¢³¤ÅþÁ¬Í¨Ñ¶¡¢ÓÎϷЧÀͶ˵ȡ£ÏÂÃæÎÒÃǾÍÒ»ÆðÀ´¿´¼¸¸öSwooleµÄÏêϸӦÓó¡¾°¡£
WebSocketЧÀÍÆ÷
ÔÚWebÓ¦ÓÃÖУ¬Ð§ÀÍÆ÷¿ÉÒÔͨ¹ýWebSocketÐÒéÓë¿Í»§¶Ë½¨É賤ÅþÁ¬¡£Ê¹ÓÃSwooleÎÒÃÇ¿ÉÒÔºÜÈÝÒ׵شWebSocketЧÀÍÆ÷£¬ÌṩʵʱͨѶЧÀÍ¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄWebSocketЧÀÍÆ÷ʵÏÖ¡£
$server = new SwooleWebSocketServer("0.0.0.0", 9501); $server->on('open', function (SwooleWebSocketServer $server, SwooleHttpRequest $request) { echo "ÅþÁ¬Òѽ¨Éè "; }); $server->on('message', function (SwooleWebSocketServer $server, SwooleWebSocketFrame $frame) { echo "ÊÕµ½ÐÂÎÅ£º{$frame->data} "; $server->push($frame->fd, "ÎÒÊÕµ½ÁËÄãµÄÐÂÎÅ£¡"); }); $server->on('close', function (SwooleWebSocketServer $server, $fd) { echo "ÅþÁ¬ÒÑ¹Ø±Õ "; }); $server->start();
µÇ¼ºó¸´ÖÆ
HTTPЧÀÍÆ÷
³ýÁËÌṩWebSocketЧÀÍÍ⣬Swoole»¹¿ÉÒÔ×÷ΪHTTPЧÀÍÆ÷¡£Óë¹Å°åµÄApache»òNginxµÈWebЧÀÍÆ÷Ïà±È£¬Ê¹ÓÃSwoole¿ÉÒÔÌá¸ßÇëÇóµÄÏìÓ¦ËÙÂʺʹ¦Àí²¢·¢ÇëÇóµÄÄÜÁ¦£¬Ìá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜÌåÏÖ¡£
ÏÂÃæÊÇSwoole×÷ΪHTTPЧÀÍÆ÷µÄÒ»¸ö¼òÆÓʾÀý¡£
$server = new SwooleHttpServer("0.0.0.0", 9501); $server->on('request', function (SwooleHttpRequest $request, SwooleHttpResponse $response) { $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("<h1>Hello, World!</h1>"); }); $server->start();
µÇ¼ºó¸´ÖÆ
׼ʱÆ÷
׼ʱʹÃüÊÇÐí¶àÓ¦ÓóÌÐò¶¼ÐèÒªµÄ³£¼û¹¦Ð§¡£SwooleÌṩÁË»ùÓÚ׼ʱÆ÷µÄÒ첽ʹÃü´¦Àí»úÖÆ£¬¿ÉÒÔÇáËÉ´¦Àí׼ʱʹÃü¡£
ÏÂÃæÊÇÒ»¸ö׼ʱÆ÷µÄʾÀý£¬Ã¿¸ô1ÃëÊä³öÒ»´Î hello world¡£
SwooleTimer::tick(1000, function () { echo "hello world "; });
µÇ¼ºó¸´ÖÆ
¶þ¡¢SwooleÊÖÒÕÒªµã
ÏàʶÁËSwooleµÄÓ¦Óó¡¾°Ö®ºó£¬ÎÒÃÇÀ´×ܽáÒ»ÏÂÈ«Õ»¹¤³ÌʦÐèÒªÕÆÎÕµÄSwooleÊÖÒÕÒªµã¡£
»ù±¾Óï·¨
×÷ΪһÃûÈ«Õ»¹¤³Ìʦ£¬ÎÒÃÇÐèÒªÕÆÎÕSwooleµÄ»ù±¾Óï·¨¡£°üÀ¨Ð§ÀÍÆ÷µÄ½¨Éè¡¢ÊÂÎñµÄ»Øµ÷º¯ÊýµÈ¡£
Òì²½I/O
SwooleʹÓÃÒì²½I/OÊÖÒÕÌá¸ßЧÀÍÆ÷µÄ²¢·¢´¦ÀíÄÜÁ¦¡£Òò´Ë£¬Ã÷È·ºÍÕÆÎÕÒì²½I/O±à³ÌÄ£×ÓÊǺÜÊÇÖ÷ÒªµÄ¡£ÔÚʹÓÃSwoole¾ÙÐÐÍøÂç±à³Ìʱ£¬ÎÒÃÇÐèҪʹÓÃг̡¢ÊÂÎñ»Øµ÷µÈÊÖÒÕÀ´ÊµÏÖÒì²½I/O¡£
¶àÀú³Ì
SwooleʹÓöàÀú³ÌÊÖÒÕʵÏÖÁ˶àÀú³Ì²¢·¢£¬¿ÉÒÔÌṩ¸ü¸ßµÄÔËÐÐЧÂÊ¡£Òò´Ë£¬×÷ΪһÃûÈ«Õ»¹¤³Ìʦ£¬ÎÒÃÇÐèÒªÕÆÎÕSwooleµÄ¶àÀú³Ì±à³ÌÄ£×Ó£¬ÏàʶÀú³Ì¡¢Àú³Ì¼äͨѶµÈÏà¹Ø֪ʶ¡£
Êý¾Ý¿â²Ù×÷
ÔÚÏÖʵӦÓÃÖУ¬¾³£ÐèÒª¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£SwooleÌṩÁËÏìÓ¦µÄÊý¾Ý¿âÀ©Õ¹£¬¿ÉÒÔ¿ì½ÝµØ¾ÙÐÐÊý¾Ý¿âÅþÁ¬ºÍÅÌÎʲÙ×÷¡£ÕÆÎÕSwooleµÄÊý¾Ý¿â²Ù×÷ÊÖÒÕ£¬¿ÉÒÔÌá¸ßЧÀÍÆ÷ÐÔÄܺͱà³ÌЧÂÊ¡£
ÄÚ´æÖÎÀí
SwooleʹÓÃÄÚ´æ³ØÊÖÒÕÀ´ÖÎÀíÄڴ棬ÌṩÁ˸ßЧµÄÄÚ´æ·ÖÅɺͽÓÄÉ»úÖÆ¡£ÕÆÎÕSwooleµÄÄÚ´æÖÎÀíÊÖÒÕ£¬¿ÉÒÔ×èÖ¹ÄÚ´æ×ß©ºÍÐÔÄÜÎÊÌâ¡£
Èý¡¢×ܽá
SwooleÊÇÒ»¸ö¸ßÐÔÄܵÄPHPÍøÂç¿ò¼Ü£¬ÊÊÓÃÓÚÖÖÖָ߲¢·¢¡¢´óÁ÷Á¿µÄÓ¦ÓóÌÐò¡£×÷Ϊȫջ¹¤³Ìʦ£¬ÎÒÃÇÐèÒªÕÆÎÕSwooleµÄ»ù±¾Óï·¨¡¢Òì²½I/O¡¢¶àÀú³Ì¡¢Êý¾Ý¿â²Ù×÷ºÍÄÚ´æÖÎÀíµÈÊÖÒÕ¡£Í¨¹ýѧϰºÍʵ¼ù£¬ÎÒÃÇ¿ÉÒÔÔËÓÃSwoole¹¹½¨¸ßÐÔÄÜ¡¢¸ß²¢·¢¡¢Îȹ̿ɿ¿µÄWebÓ¦ÓóÌÐò£¬Ìá¸ßÓ¦ÓóÌÐòµÄÍÌÍÂÁ¿ºÍÐÔÄÜÌåÏÖ¡£
ÒÔÉϾÍÊÇÈ«Õ»¹¤³Ìʦ±Ø±¸ÊÖÒÕÖ®SwooleÊÃ÷ÈսƪµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡