Swooleʵ¼ùÂÄÀú£ºÊ¹ÓÃгÌÌáÉýÄ£°åäÖȾÐÔÄÜ
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬ÍøÕ¾µÄ»á¼ûÁ¿Ô½À´Ô½´ó£¬ÍøÕ¾µÄ²¢·¢ÄÜÁ¦³ÉΪÁËÍøÕ¾Éè¼ÆÓ뿪·¢µÄÖ÷Ҫ˼Á¿ÒòËØÖ®Ò»¡£ÎªÁËÔöÇ¿ÍøÕ¾µÄ²¢·¢ÄÜÁ¦£¬Öî¶à¹¤¾ßºÍÊÖÒÕÒ²Ó¦Ô˶øÉú¡£±¾ÎĽ«ÏÈÈÝÒ»¸öÓÃÓÚÌáÉýwebÓ¦ÓóÌÐò²¢·¢ÄÜÁ¦µÄphpÀ©Õ¹¿â¡ª¡ªswooleµÄÒ»¸öʵ¼ù°¸Àý£¬×ÝÈ»ÓÃгÌÌáÉýÄ£°åäÖȾÐÔÄÜ¡£
Ò»¡¢Ê²Ã´ÊÇSwoole£¿
SwooleÊÇPHPÓïÑÔµÄÒ»¸öCÀ©Õ¹£¬Ëü½«PHPµ±×öËüµÄÀ©Õ¹ÓïÑÔ£¬ÔÚPHPÖÐÌṩǿʢµÄÒì²½¡¢²¢ÐС¢¸ßÐÔÄÜ¡¢Ð³ÌµÈ¹¦Ð§¡£Ê¹ÓÃSwoole¿ÉÒÔ¿ª·¢¸ßÐÔÄܵÄÍøÂçЧÀÍÆ÷¡¢WebÓ¦Óá¢API¡¢´ó¹æģ΢ЧÀͺÍÎïÁªÍøµÈÓ¦ÓóÌÐò£¬Ê¹PHPµÄÓ¦ÓóÌÐòÓиü¶àµÄ¿ÉÄÜÐÔ¡£
¶þ¡¢Ð³ÌÊÖÒÕ
гÌÊÇÒ»ÖÖÓû§Ä£Ê½Ị̈߳¬²»ÐèÒª²Ù×÷ϵͳ¾ÙÐÐÏß³ÌÇл»ºÍÉÏÏÂÎÄÉúÑĻָ´µÄÖÐÐļÛÇ®£¬¿ÉÒÔÔÚµ¥Ïß³ÌÄÚ²¿ÊµÏÖ¶à¸öʹÃü¼äµÄÇл»Ö´ÐУ¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄ²¢·¢ÄÜÁ¦ºÍÐÔÄÜ¡£Ä¿½ñÊÐÃæÉÏÊ¢ÐеÄг̿ò¼ÜÓÐSwow¡¢Hyperf¡¢SwoftµÈ¡£
Èý¡¢ÎªÊ²Ã´ÒªÊ¹ÓÃгÌÌáÉýÄ£°åäÖȾÐÔÄÜ£¿
Ä£°åäÖȾÊÇWeb¿ª·¢Öбز»¿ÉÉÙµÄÒ»¸ö»·½Ú£¬Ðí¶àWeb¿ª·¢¿ò¼ÜÒ²¶¼ÌṩÁËÄ£°åäÖȾ¹¦Ð§¡£Ä£°åÒýÇæÔÚÆÊÎöÄ£°åÎļþʱ£¬ÈôÊÇʹÓÃÁËÕýÔò±í´ïʽ¡¢if-else¡¢forµÈÁ÷³Ì¿ØÖÆÓï¾ä£¬ÄÇôģ°åäÖȾµÄÐÔÄܽÏÁ¿µÍÏ¡£Ôڸ߲¢·¢ÇéÐÎÏ£¬Ò»µ©Ä£°åäÖȾʹÃü´ó×ÚÛÕ±Õ»òºÄʱ½Ï³¤£¬¾Í»áµ¼ÖÂЧÀÍÆ÷ÏìÓ¦ËÙÂÊÂý£¬ÉõÖÁÒýÆðЧÀÍÆ÷Í߽⡣ʹÓÃгÌÀ´ÓÅ»¯Ä£°åäÖȾ£¬¿ÉÒÔ´ó´óÌá¸ßÄ£°åäÖȾµÄÐÔÄÜ¡£
ËÄ¡¢ÔõÑùʹÓÃгÌÓÅ»¯Ä£°åäÖȾ£¿
1.Ê×ÏÈÐèÒªÔÚWebÓ¦ÓóÌÐòµÄÈë¿ÚÎļþÖÐÒýÈëSwooleÀ©Õ¹ºÍÆô¶¯Ð§ÀÍÆ÷£º
php <?php //ÒýÈëSwooleÀ©Õ¹ use SwooleHttpRequest; use SwooleHttpResponse; use SwooleHttpServer; //½¨ÉèWebЧÀÍÆ÷ $server = new Server("0.0.0.0", 9501); //¼àÌýÇëÇó $server->on("request", function (Request $request, Response $response) { //»ñÈ¡Ä£°åÄÚÈÝ $content = file_get_contents("./template/index.html"); //Ì滻ģ°å±äÁ¿ $content = str_replace("{name}", "Swooleʵ¼ù", $content); //Êä³öÏìÓ¦ $response->header("Content-Type", "text/html; charset=utf-8"); $response->end($content); }); //Æô¶¯Ð§ÀÍÆ÷ $server->start();
µÇ¼ºó¸´ÖÆ
2.ʹÓÃгÌÓÅ»¯Ä£°åäÖȾ
php <?php use SwooleHttpRequest; use SwooleHttpResponse; use SwooleHttpServer; use SwooleCoroutine; //½ç˵äÖȾº¯Êý£¬Ê¹ÓÃÐ³Ì function render($content, $var) { return Coroutineun(function() use ($content, $var) { foreach ($var as $k => $v) { $content = str_replace("{{$k}}", $v, $content); } return $content; }); } //½¨ÉèWebЧÀÍÆ÷ $server = new Server("0.0.0.0", 9501); //¼àÌýÇëÇó $server->on("request", function (Request $request, Response $response) { //¶ÁÈ¡Ä£°åÎļþÄÚÈÝ $content = file_get_contents("./template/index.html"); //äÖȾģ°å $content = render($content, ["name" => "Swooleʵ¼ù"]); //ÏìӦЧ¹û $response->header("Content-Type", "text/html; charset=utf-8"); $response->end($content); }); //Æô¶¯Ð§ÀÍÆ÷ $server->start();
µÇ¼ºó¸´ÖÆ
ÈçÉÏ´úÂëÔÚÄ£°åäÖȾʱʹÓÃг̣¬´ó´óÌá¸ßÁËÄ£°åäÖȾµÄ²¢·¢ÄÜÁ¦£¬Ïà±ÈÓÚ֮ǰµÄ´úÂ룬ÒýÈëÁËSwooleµÄг̣¬ÔÚäÖȾµÄʱ¼ä²»»áÛÕ±ÕỊ̈߳¬ÔËÐÐÔ½·¢¸ßЧ¡£
Îå¡¢×ܽá
±¾ÎÄÏÈÈÝÁËʹÓÃSwooleÀ©Õ¹ºÍгÌÊÖÒÕÓÅ»¯Ä£°åäÖȾµÄÒªÁ졣ʹÓÃSwooleÀ©Õ¹²»µ«¿ÉÒÔÌṩÒì²½¡¢²¢ÐС¢¸ßÐÔÄÜ¡¢Ð³ÌµÈ¹¦Ð§£¬»¹¿ÉÒÔʹÓÃÕâЩÌØÕ÷À´Ìá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄܺͲ¢·¢ÄÜÁ¦¡£Ê¹ÓÃгÌÀ´ÓÅ»¯Ä£°åäÖȾºÍÆäËûÐèÒªÒì²½´¦ÀíµÄʹÃü£¬¿ÉÒÔÌá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄܺÍÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇSwooleʵ¼ùÂÄÀú£ºÊ¹ÓÃгÌÌáÉýÄ£°åäÖȾÐÔÄܵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡