尊龙凯时人生就是搏

SwooleÍøÂç±à³Ì»ù´¡ÈëÃÅÖ¸ÄÏ

Ëæ×Å»¥ÁªÍøµÄÒ»Ö±Éú³¤ºÍÆÕ¼°£¬ÍøÂç±à³ÌÊÖÒÕÒ²³ÉΪÁËÐí¶à³ÌÐòÔ±±Ø±¸µÄÊÖÒÕÖ®Ò»¡£ÔÚÕâ¸öÁìÓòÖУ¬swooleÊÇÒ»¿îÊ®·ÖÓÅÒìµÄÍøÂçͨѶ¿ò¼Ü¡£swooleÊÇphpµÄÀ©Õ¹Ä£¿é£¬ÌṩÁËÒì²½io¡¢¶àÀú³Ì¡¢Ð­³ÌµÈÇ¿Ê¢µÄÍøÂç±à³Ì¹¦Ð§£¬¿ÉÒԺܺõؽâ¾ö¸ß²¢·¢¡¢¸ßÐÔÄܵÈÎÊÌâ¡£±¾ÎĽ«Îª¸÷ÈËÏÈÈÝswooleµÄÍøÂç±à³Ì»ù´¡ÈëÃÅÖ¸ÄÏ¡£

Ò»¡¢SwooleµÄ×°ÖÃÓëÉèÖÃ

SwooleµÄ×°ÖÃÐèÒªPHP°æ±¾´óÓÚ7.0£¬²¢ÇÒÐèҪװÖÃphpizeºÍphp-config£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº

$ git clone https://github.com/swoole/swoole-src.git
$ cd swoole-src
$ phpize
$ ./configure
$ make && make install

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê³Éºó£¬ÔÚphp.iniÖÐÌí¼ÓÒÔÏÂÉèÖãº

extension=swoole.so

µÇ¼ºó¸´ÖÆ

¶þ¡¢SwooleµÄ»ù±¾Ê¹ÓÃ

1.½¨ÉèTCPЧÀÍÆ÷

Swoole¿ÉÒÔͨ¹ýÒÔÏ´úÂ뽨ÉèÒ»¸öTCPЧÀÍÆ÷£¬¼àÌý±¾»úµÄ9501¶Ë¿Ú£º

$server = new SwooleServer("0.0.0.0", 9501);

µÇ¼ºó¸´ÖÆ

2.¼àÌýÊÂÎñ

ЧÀÍÆ÷ÐèÒª¼àÌý¿Í»§¶ËµÄÅþÁ¬¡¢ÎüÊÕÊý¾Ý¡¢¹Ø±ÕÅþÁ¬µÈÊÂÎñ¡£¿ÉÒÔͨ¹ýÒÔÏ´úÂë¾ÙÐмàÌý£º

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

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

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

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУ¬onÒªÁìÓÃÓÚ°ó¶¨ÊÂÎñÃû³ÆºÍ»Øµ÷º¯Êý¡£

3.Æô¶¯Ð§ÀÍÆ÷

ÔÚÍê³ÉÊÂÎñµÄ¼àÌýºó£¬ÐèÒªÔËÐÐÒÔÏ´úÂëÆô¶¯Ð§ÀÍÆ÷£º

$server->start();

µÇ¼ºó¸´ÖÆ

´Ëʱ£¬ÒѾ­Àֳɽ¨ÉèÁËÒ»¸öTCPЧÀÍÆ÷¡£¿ÉÒÔͨ¹ýtelnetµÈ¹¤¾ß¾ÙÐвâÊÔ¡£

3.½¨ÉèUDPЧÀÍÆ÷

SwooleÒ²¿ÉÒÔ½¨ÉèÒ»¸öUDPЧÀÍÆ÷£¬Ê¹ÓÃÒªÁìÓëTCPЧÀÍÆ÷ÀàËÆ¡£ÒÔÏÂÊǽ¨ÉèUDPЧÀÍÆ÷µÄʾÀý´úÂ룺

$server = new SwooleServer("0.0.0.0", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

$server->on('Packet', function ($server, $data, $clientInfo) {
    $server->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
});

$server->start();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУ¬½¨ÉèÁËÒ»¸öUDPЧÀÍÆ÷£¬¼àÌý±¾»úµÄ9502¶Ë¿Ú¡£µ±ÓÐÊý¾Ý·¢Ë͵½Ð§ÀÍÆ÷ʱ£¬»á´¥·¢PacketÊÂÎñ£¬²¢½«Êý¾Ý·¢Ëͻؿͻ§¶Ë¡£

4.½¨ÉèWebSocketЧÀÍÆ÷

Swoole»¹¿ÉÒÔ½¨ÉèWebSocketЧÀÍÆ÷£¬WebSocketÊÇÒ»ÖÖ»ùÓÚTCPЭÒéµÄÈ«Ë«¹¤Í¨Ñ¶Ð­Òé¡£ÒÔÏÂÊǽ¨ÉèWebSocketЧÀÍÆ÷µÄʾÀý´úÂ룺

$server = new SwooleWebSocketServer("0.0.0.0", 9503);

$server->on('open', function (SwooleWebSocketServer $server, $request) {
    echo "server: handshake success with fd{$request->fd}
";
});

$server->on('message', function (SwooleWebSocketServer $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->start();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУ¬½¨ÉèÁËÒ»¸öWebSocketЧÀÍÆ÷£¬¼àÌý±¾»úµÄ9503¶Ë¿Ú¡£µ±Óпͻ§¶ËÅþÁ¬Ê±£¬»á´¥·¢openÊÂÎñ¡£µ±Óпͻ§¶Ë·¢ËÍÐÂÎÅʱ£¬»á´¥·¢messageÊÂÎñ£¬²¢½«ÐÂÎÅÔ­Ñù·¢Ëͻؿͻ§¶Ë¡£µ±Óпͻ§¶Ë¹Ø±ÕÅþÁ¬Ê±£¬»á´¥·¢closeÊÂÎñ¡£

Èý¡¢SwooleµÄ½ø½×ʹÓÃ

1.ʹÓÃTaskÒ첽ʹÃü

SwooleÌṩµÄTask¹¦Ð§¿ÉÒÔ½«Ò»Ð©ºÄʱµÄÓªÒµÂß¼­Òì²½´¦Àí£¬²»»áÛÕ±ÕÖ÷Àú³ÌµÄÔËÐС£ÒÔÏÂÊÇTaskµÄʾÀý´úÂ룺

$server = new SwooleServer("0.0.0.0", 9501);

$server->on('receive', function ($serv, $fd, $from_id, $data) {
    $task_id = $serv->task($data); //ͶµÝÒ첽ʹÃü
    echo "Dispath AsyncTask: id=$task_id
";
});

$server->on('task', function ($serv, $task_id, $from_id, $data) {
    echo "New AsyncTask[id=$task_id]".PHP_EOL;
    // ´¦ÀíÒ첽ʹÃü
    $serv->finish("$data -> OK");
});

$server->on('finish', function ($serv, $task_id, $data) {
    echo "AsyncTask[$task_id] Finish: $data".PHP_EOL;
});

$server->start();

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏʾÀý´úÂëÖУ¬µ±Óпͻ§¶Ë·¢ËÍÊý¾Ýµ½Ð§ÀÍÆ÷ʱ£¬»á½«Ê¹ÃüͶµÝµ½TaskÐÐÁÐÖУ¬²¢ÔÚonTaskÊÂÎñÖд¦ÀíÒ첽ʹÃü¡£Task´¦ÀíÍê³Éºó£¬»áŲÓÃonFinishÊÂÎñ½«´¦ÀíЧ¹û·µ»Ø¸ø¿Í»§¶Ë¡£

2.ʹÓÃЭ³Ì

Э³ÌÊÇSwooleÌṩµÄÒ»ÖÖ²¢·¢±à³Ì·½·¨£¬¿ÉÒÔÔÚÒ»¸öÏß³ÌÖÐʵÏÖÍòÍò¼¶±ðµÄ²¢·¢´¦Àí¡£ÒÔÏÂÊÇʹÓÃЭ³ÌµÄʾÀý´úÂ룺

Coun(function () {
    $client = new SwooleCoroutineClient(SWOOLE_SOCK_TCP);

    if (!$client->connect('127.0.0.1', 9501, 0.5)) {
        echo "connect failed. Error: {$client->errCode}
";
    }

    $client->send("hello swoole");

    $res = $client->recv();
    echo $res;

    $client->close();
});

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏʾÀý´úÂëÖУ¬Ê¹ÓÃCoun½¨ÉèÁËÒ»¸öЭ³ÌʹÃü£¬Í¨¹ýSwooleCoroutineClient½¨ÉèÒ»¸öTCP¿Í»§¶Ë£¬²¢Ê¹ÓÃconnectÒªÁì¾ÙÐÐÅþÁ¬¡£µ±ÅþÁ¬½¨ÉèÀÖ³ÉÖ®ºó£¬Ê¹ÓÃsendÒªÁì·¢ËÍÒ»ÌõÐÂÎÅ£¬²¢Ê¹ÓÃrecvÒªÁìÎüÊÕ·µ»ØЧ¹û¡£×îºóʹÓÃcloseÒªÁì¹Ø±ÕÅþÁ¬¡£

ËÄ¡¢×ܽá

±¾ÎÄÏÈÈÝÁËSwooleÍøÂç±à³Ì¿ò¼ÜµÄ»ù±¾Ê¹ÓÃÒªÁ죬²¢Í¨¹ýʾÀý´úÂëչʾÁËTCPЧÀÍÆ÷¡¢UDPЧÀÍÆ÷¡¢WebSocketЧÀÍÆ÷¡¢TaskÒ첽ʹÃü¡¢Ð­³ÌµÈ¹¦Ð§µÄʹÓÃÒªÁì¡£Swoole¾ßÓÐÎÞаÐԺ͸ßÐÔÄÜ£¬ÔÚÐí¶à³¡¾°Ï¿ÉÒÔÆðµ½ÓÅÒìµÄЧ¹û¡£¿ÉÊÇ£¬Ò²ÐèÒª¿ª·¢Ö°Ô±ÓÐÒ»¶¨µÄµ×²ã֪ʶºÍÕë¶ÔÐԵıà³ÌÍ·ÄÔ¡£ÐÅÍжÁÕßͨ¹ý±¾ÎĵÄÏÈÈÝ£¬¿ÉÒÔ¶ÔSwooleÓÐÒ»¸öÆðÔ´µÄÊìϤºÍÓ¦Óá£

ÒÔÉϾÍÊÇSwooleÍøÂç±à³Ì»ù´¡ÈëÃÅÖ¸ÄϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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