尊龙凯时人生就是搏

Swooleʵ¼ù£º¹¹½¨¸ßÐÔÄܵÄ΢ЧÀÍ¿ò¼Ü

Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬Î¢Ð§Àͼܹ¹ÒѾ­³ÉΪÁËÄ¿½ñµÄÇ÷ÊÆ¡£Î¢Ð§ÀÍÊÇÒ»ÖÖÉè¼ÆÀíÄͨ¹ý½«Ò»¸ö´óÐ͵ļòµ¥Ó¦ÓóÌÐò¾ÙÐвð·Ö³É¶à¸öСÐÍÓ¦ÓóÌÐòÀ´ÊµÏÖÄ£¿é»¯¿ª·¢¡£ÕâÖÖģʽʹµÃÍŶӿÉÒÔ¸ü¿ìµØ¹¹½¨ºÍ°²ÅÅÖØ´óµÄϵͳ£¬Í¬Ê±Ò²¾ß±¸¸üºÃµÄ¿ÉÀ©Õ¹ÐԺͿÉά»¤ÐÔ¡£

ÔÚÕâÀÎÒÃǽ«ÏÈÈÝÒ»ÖÖʹÓÃSwooleʵÏÖ¸ßÐÔÄÜ΢ЧÀͼܹ¹µÄÒªÁì¡£SwooleÊÇÒ»ÖÖ¿ªÔ´µÄ¸ßÐÔÄÜÍøÂç¿ò¼Ü£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÒì²½IO¡¢Ð­³ÌºÍTCP/UDPÍøÂç±à³ÌµÈ¹¦Ð§¡£Ëü¾ßÓÐÓÅÒìµÄÐÔÄܺÍÎȹÌÐÔ£¬Ê¹µÃËü³ÉΪ¹¹½¨¸ßÐÔÄÜ΢ЧÀͼܹ¹µÄÀíÏëÑ¡Ôñ¡£

ΪÁËÑÝʾSwooleÔÚ¹¹½¨Î¢Ð§ÀÍÖеÄÓ¦Óã¬ÎÒÃǽ«½¨ÉèÒ»¸ö¼òÆÓµÄ΢ЧÀÍÓ¦ÓóÌÐò£¬°üÀ¨APIÍø¹ØºÍ¶à¸öЧÀͽڵã¡£ËùÓÐЧÀͽڵ㽫ʹÓÃSwooleʵÏÖÒ첽ͨѶºÍ¸ßÐÔÄÜ´¦Àí¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÉè¼ÆÒ»¸öAPIÍø¹Ø£¬½«¿Í»§¶ËÇëÇóת·¢µ½²î±ðµÄЧÀͽڵã¡ £»ùÓÚSwooleµÄHTTPЧÀÍÆ÷ÌṩÁËÒ»ÖÖ¼òÆӵķ½·¨À´ÊµÏÖÕâ¸ö¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔ±àдһ¸ö¼òÆÓµÄHTTPЧÀÍÆ÷£¬´¦Àí¿Í»§¶ËÇëÇó²¢½«ËüÃÇ·Óɵ½²î±ðµÄЧÀÍ¡£

ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«Ê¹ÓÃÒÔÏ·ÓÉ£º

/user/:id – »ñÈ¡Ò»¸öÌض¨Óû§µÄÏêϸÐÅÏ¢

/product/:id – »ñÈ¡Ò»¸öÌض¨ÉÌÆ·µÄÏêϸÐÅÏ¢

ÔÚ·ÓÉÖУ¬:idÊÇÒ»¸ö²ÎÊý£¬ÓÃÓÚ¶¨Î»Ìض¨µÄÓû§»òÉÌÆ·¡£ÎÒÃǽ«ÔÚÎÒÃǵÄЧÀͽڵãÖÐʵÏÖÕâЩAPI¡£

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨Éè¶à¸öЧÀͽڵ㣬ËüÃǽ«´¦ÀíAPIÇëÇó²¢ÏìÓ¦¿Í»§¶Ë¡£ÓÉÓÚSwooleµÄÒì²½IOºÍЭ³ÌÖ§³Ö£¬ÎÒÃÇ¿ÉÒÔʹÓÃËüÀ´ÊµÏÖ¿ìËÙÏìÓ¦µÄЧÀͶË¡£

ÎÒÃǽ«½¨ÉèÁ½¸öЧÀͽڵ㣺Óû§Ð§ÀͺÍÉÌƷЧÀÍ¡£ÎªÁ˼ò»¯Õâ¸öÀý×Ó£¬ÎÒÃǽ«Ê¹ÓÃSQLite×÷ΪÎÒÃǵÄÊý¾Ý¿â¡£

Óû§Ð§Àͽ«ÌṩÒÔÏÂAPI£º

/user/:id – »ñÈ¡Ò»¸öÌض¨Óû§µÄÏêϸÐÅÏ¢

/user/:id/orders – »ñÈ¡Ò»¸öÌض¨Óû§µÄËùÓж©µ¥

ÉÌƷЧÀͽ«ÌṩÒÔÏÂAPI£º

/product/:id – »ñÈ¡Ò»¸öÌض¨ÉÌÆ·µÄÏêϸÐÅÏ¢

ÎÒÃǽ«ÑÝʾÔõÑùʹÓÃSwoole½¨ÉèÒ»¸ö»ù±¾µÄЧÀͽڵã¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö»ù´¡µÄSwooleЧÀÍÆ÷£¬²¢¼àÌýÖ¸¶¨µÄ¶Ë¿Ú¡£

$server = new SwooleServer('0.0.0.0', 8001, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->on('Receive', function (SwooleServer $server, $fd, $reactor_id, $data) {

});
$server->start();

µÇ¼ºó¸´ÖÆ

µ±ÓÐÇëÇóµÖ´ïʱ£¬ÎÒÃǽ«ÔÚon(‘receive’)»Øµ÷Öд¦ÀíËü¡£ÎÒÃǽ«ÆÊÎöHTTPÇëÇó²¢ÌáÈ¡ÓÐÓõÄÐÅÏ¢£¬ÈçURLºÍHTTPÒªÁì¡£

$server->on('Receive', function (SwooleServer $server, $fd, $reactor_id, $data) {
    // ÆÊÎöHTTPÇëÇó
    $header_end_pos = strpos($data, "

");
    $header_data = substr($data, 0, $header_end_pos);
    $body_data = substr($data, $header_end_pos + 4);
    $http_parser = new SwooleHttpParser();
    $request = $http_parser->execute($header_data, $body_data);
    
    // ÌáÈ¡URLºÍHTTPÒªÁì
    $path_info = isset($request['server']['path_info']) ? $request['server']['path_info'] : '/';
    $http_method = isset($request['server']['request_method']) ? $request['server']['request_method'] : 'GET';
});

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´£¬ÎÒÃǽ«´¦ÀíÕâЩÇëÇó²¢·µ»ØÏìÓ¦¡£ÎÒÃÇÐèҪʹÓÃSwooleµÄЭ³ÌÌØÕ÷ÔÚͳһ¸öÇëÇó´¦ÀíÖÜÆÚÄÚʵÏÖÒì²½IO¡£ÔÚÎÒÃǵÄÀý×ÓÖУ¬ÎÒÃǽ«ÅÌÎÊSQLiteÊý¾Ý¿âÀ´»ñÈ¡Óû§ºÍÉÌÆ·ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔʹÓÃЭ³Ì¿Í»§¶ËÀ©Õ¹À´Ö´ÐÐÒì²½ÅÌÎʺÍдÈë¡£×îºó£¬ÎÒÃǽ«·µ»ØÒ»¸ö´øÓÐ׼ȷHTTP״̬ÂëºÍÏìÓ¦ÄÚÈݵÄHTTPÏìÓ¦¡£

$server->on('Receive', function (SwooleServer $server, $fd, $reactor_id, $data) {
    // ÆÊÎöHTTPÇëÇó
    ...
    
    // ´¦ÀíÇëÇó
    $response = ['status' => 404, 'content' => 'Not Found'];
    if (preg_match('/^/user/(d+)/', $path_info, $matches)) {
        // ÅÌÎÊÓû§ÐÅÏ¢
        $user_id = $matches[1];
        $db = new SwooleCoroutineMySQL();
        $db->connect([
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'root',
            'password' => 'password',
            'database' => 'test'
        ]);
        $result = $db->query("SELECT * FROM users WHERE id = '{$user_id}'");

        // ÌìÉúÏìÓ¦
        if ($result) {
            $response = ['status' => 200, 'content' => json_encode($result->fetch(), JSON_UNESCAPED_UNICODE)];
        } else {
            $response = ['status' => 404, 'content' => 'Not Found'];
        }

        $db->close();
    } else if (preg_match('/^/user/(d+)/orders/', $path_info, $matches)) {
        // ÅÌÎÊÓû§¶©µ¥
        ...

        // ÌìÉúÏìÓ¦
        ...
    } else if (preg_match('/^/product/(d+)/', $path_info, $matches)) {
        // ÅÌÎÊÉÌÆ·ÐÅÏ¢
        ...

        // ÌìÉúÏìÓ¦
        ...
    }

    // ·¢ËÍÏìÓ¦
    $http_response = new SwooleHttpResponse();
    $http_response->status($response['status']);
    $http_response->header('Content-Type', 'application/json');
    $http_response->end($response['content']);
});

µÇ¼ºó¸´ÖÆ

ÕâÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ËµÃ÷ÎúÔõÑùʹÓÃSwooleÀ´¹¹½¨¸ßÐÔÄܵÄ΢ЧÀͼܹ¹¡£SwooleÌṩÁËÐí¶à¹¦Ð§ºÍ¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¿ìËÙÏìÓ¦¡¢¸ßÐÔÄܺͿÉÀ©Õ¹µÄ΢ЧÀÍÓ¦ÓóÌÐò¡£

ÔÚʹÓÃSwooleʱ£¬ÎÒÃÇÐèҪעÖØÒÔϼ¸µã£º

ʼÖÕ¼á³Ö´úÂ뾫Á·ºÍÒ×ÓÚÃ÷È·¡£

³ä·ÖʹÓÃSwooleµÄÒì²½IOºÍЭ³ÌÌØÕ÷£¬ÒÔʵÏÖ¸ßÐÔÄÜ´¦ÀíºÍÏìÓ¦¡£

½«¶à¸öЧÀͽڵãÍŽáÆðÀ´£¬Ê¹ÓÃAPIÍø¹ØͳһÖÎÀíºÍ·ÓÉÇëÇó¡£

ͨ¹ýÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔÇáËɵع¹½¨¸ßÐÔÄܵÄ΢ЧÀͼܹ¹£¬ÎªÎÒÃǵÄÓ¦ÓóÌÐòÌṩ¸üºÃµÄ¿ÉÀ©Õ¹ÐÔ¡¢¿Éά»¤ÐԺͿɿ¿ÐÔ¡£

ÒÔÉϾÍÊÇSwooleʵ¼ù£º¹¹½¨¸ßÐÔÄܵÄ΢ЧÀÍ¿ò¼ÜµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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