SwooleʵÏÖ¸ßÐÔÄܵÄÉç½»¹¦Ð§
Ëæ×ÅÉ罻ýÌåµÄ·ÉËÙÉú³¤£¬Ô½À´Ô½¶àµÄÆóÒµºÍСÎÒ˽¼ÒÐèÒªÔÚÍøÕ¾»òÓ¦ÓÃÖÐʵÏÖÉç½»¹¦Ð§£¬ÒÔ±ã¸üºÃµØÓëÓû§»¥¶¯ºÍÏàͬ¡£¶øΪÁËʵÏָ߲¢·¢¡¢µÍÑÓ³ÙµÄÉç½»¹¦Ð§£¬¿ª·¢ÕßÃÇÐèҪѡÔñһЩ¸ßÐÔÄܵŤ¾ßºÍ¿ò¼Ü¡£ÆäÖУ¬swoole¾ÍÊÇÒ»¸öºÜ²»´íµÄÑ¡Ôñ¡£
SwooleÊÇÒ»¿î»ùÓÚPHPµÄÒì²½¡¢¸ßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬ËüÖ¼ÔÚÌá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄÜ£¬ÌØÊâÊÇÔÚ´¦Öóͷ£¸ß²¢·¢ÇëÇóʱ¡£Swoole¿ÉÒÔÓëPHPµÄͨÀýÓïÑÔÔªËØÎ޷켯³É£¬Í¬Ê±»¹ÌṩÁËÖ±½Ó²Ù×÷µ×²ãг̡¢TCP¡¢UDP¡¢UnixÌ×½Ó×Ö¡¢HTTP¡¢WebSocketµÈÍøÂçÐÒéµÄAPI£¬ÉÁ¿ª·¢Õß¿ÉÒÔÔ½·¢±ã½ÝµØʵÏÖÖÖÖÖ¸ßÐÔÄÜʹÃü¡£
ÏÂÃæÎÒÃÇÀ´Ì½ÌÖÔõÑùʹÓÃSwooleʵÏÖ¸ßÐÔÄܵÄÉç½»¹¦Ð§¡£
ʵÏÖWebSocketЧÀÍ
ÔÚʵÏÖÉç½»¹¦Ð§Ê±£¬WebSocketÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄÐÒé¡£ËüÖ§³ÖË«ÏòµÄ¡¢ÊµÊ±µÄÊý¾Ý´«Ê䣬¿ÉÒÔÈÃЧÀÍÆ÷ʵʱµØÍÆËÍÐÂΟø¿Í»§¶Ë£¬Ò²¿ÉÒÔÈÿͻ§¶ËÓëЧÀÍÆ÷ʵÏÖʵʱµÄ½»»¥¡£ÔÚSwooleÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃswoole_websocket_serverÀàÀ´ÊµÏÖWebSocketЧÀÍ¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£º
$server = new swoole_websocket_server("0.0.0.0", 9501); $server->on('open', function (swoole_websocket_server $server, $request) { echo "WebSocket¿Í»§¶Ë{$request->fd}ÒÑÅþÁ¬ "; }); $server->on('message', function (swoole_websocket_server $server, $frame) { echo "À´×Ô¿Í»§¶Ë{$frame->fd}µÄÐÂÎÅ£º{$frame->data} "; $server->push($frame->fd, "ÕâÊÇÀ´×ÔЧÀÍÆ÷µÄ»Ø¸´"); }); $server->on('close', function ($server, $fd) { echo "WebSocket¿Í»§¶Ë{$fd}ÒÑ¹Ø±Õ "; }); $server->start();
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öWebSocketЧÀÍÆ÷²¢¼àÌý9501¶Ë¿Ú£¬ÔÚ¿Í»§¶ËÅþÁ¬»ò¶Ï¿ªÊ±´òÓ¡ÈÕÖ¾ÐÅÏ¢¡£ÔÚÊÕµ½¿Í»§¶Ë·¢Ë͵ÄÐÂÎÅʱ£¬Ð§ÀÍÆ÷»á´òÓ¡³öÐÂÎÅÄÚÈݲ¢»Ø¸´Ò»ÌõÐÅÏ¢¡£
ʹÓÃг̾ÙÐÐHTTPÇëÇó²¢ÍÆËÍÐÂÎÅ
SwooleÌṩÁËг̵ÄÖ§³Ö£¬¿ÉÒÔÈÃÎÒÃÇÔ½·¢±ã½ÝµØ¾ÙÐÐHTTPÇëÇóºÍÒ첽ʹÃüµÈ²Ù×÷¡£ÔÚʵÏÖÉç½»¹¦Ð§Ê±£¬ÎÒÃǾ³£ÐèÒª¾ÙÐÐHTTPÇëÇ󣬺ñȻñÈ¡Óû§µÄСÎÒ˽¼Ò×ÊÁÏ¡¢Ö¿ÓÑÁбíµÈÐÅÏ¢¡£ÒÔÏÂÊÇÒ»¸öʹÓÃSwooleгÌHTTP¿Í»§¶ËµÄÀý×Ó£º
co(function () { $cli = new SwooleCoroutineHttpClient('www.example.com', 80); $cli->set(['timeout' => 1]); $cli->setHeaders([ 'Host' => 'www.example.com', 'User-Agent' => 'Chrome/49.0.2587.3', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'Accept-Encoding' => 'gzip', ]); $cli->get('/path/to/api'); echo $cli->body; });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃSwooleCoroutineHttpClientÀàÀ´¾ÙÐÐHTTPÇëÇó¡£Õâ¸öÀàÊÇÒ»¸öг̿ͻ§¶Ë£¬¿ÉÒÔʵÏÖÒì²½µÄHTTPÇëÇó²Ù×÷¡£ÔÚ·¢ËÍÇëÇóÇ°£¬ÎÒÃÇ¿ÉÒÔÉèÖÃÇëÇóµÄ³¬Ê±Ê±¼äºÍÇëÇóÍ·µÈÐÅÏ¢¡£Ö´ÐÐÍêÇëÇóºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ý$cli->bodyÀ´»ñÈ¡ÏìÓ¦µÄÄÚÈÝ¡£
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÔÚWebSocketЧÀÍÆ÷ÖÐʹÓÃгÌHTTPÇëÇó¿Í»§¶Ë£¬»ñÈ¡Óû§ÐÅÏ¢²¢ÍÆË͸ø¿Í»§¶Ë¡£ÀýÈ磬ÔÚ»ñÈ¡Óû§µÄСÎÒ˽¼Ò×ÊÁÏʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
$server->on('message', function (swoole_websocket_server $server, $frame) { $path = '/user/profile?id=' . $frame->data; $cli = new SwooleCoroutineHttpClient('www.example.com', 80); $cli->set(['timeout' => 1]); $cli->setHeaders([ 'Host' => 'www.example.com', 'User-Agent' => 'Chrome/49.0.2587.3', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'Accept-Encoding' => 'gzip', ]); $cli->get($path); $profile = json_decode($cli->body, true); $server->push($frame->fd, json_encode($profile)); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇͨ¹ýWebSocketЧÀÍÆ÷ÎüÊÕµ½ÁËÒ»¸öÐÂÎÅ£¬ÌåÏÖÒª»ñÈ¡Óû§µÄСÎÒ˽¼Ò×ÊÁÏ¡£ÎÒÃÇʹÓÃSwooleCoroutineHttpClientÀàÀ´¾ÙÐÐHTTPÇëÇ󣬲¢½«ÏìÓ¦µÄJSONÊý¾ÝÆÊÎö³ÉÊý×é$profile¡£×îºóÔÙ½«$profileµÄÄÚÈÝͨ¹ýWebSocketÍÆË͸ø¿Í»§¶Ë¡£
ʹÓÃSwoole Redis¿Í»§¶Ë×ö»º´æ
ÔÚʵÏÖÉç½»¹¦Ð§Ê±£¬»º´æÊÇÒ»¸ö·Ç¾³£¼ûµÄÐèÇó¡£ÎªÁËÌá¸ß¶ÁÈ¡Êý¾ÝµÄЧÂÊ£¬ÎÒÃǾ³£ÐèҪʹÓÃRedisµÈ»º´æ¹¤¾ßÀ´»º´æÊý¾Ý¡£¶øÔÚSwooleÖУ¬¿ÉÒÔʹÓÃSwoole Redis¿Í»§¶ËÀ´¿ìËÙµØÓëRedisʵÀý¾ÙÐн»»¥¡£
ÒÔÏÂÊÇÒ»¸öʹÓÃSwoole Redis¿Í»§¶ËµÄÀý×Ó£º
$redis = new SwooleCoroutineRedis(); $redis->connect('127.0.0.1', 6379); $redis->set('key', 'value'); $value = $redis->get('key');
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃSwooleCoroutineRedisÀàÀ´ÊµÏÖRedis¿Í»§¶ËµÄ¹¦Ð§£¬¿ÉÒÔºÜÊÇÀû±ãµØ¾ÙÐÐÊý¾ÝµÄ¶ÁÈ¡ºÍдÈë²Ù×÷¡£
¹ØÓÚ»º´æµÄÓ¦Óó¡¾°£¬ÀýÈçÔÚ»ñÈ¡Ö¿ÓÑÁбíʱ£¬ÎÒÃÇ¿ÉÒÔ½«Êý¾Ý»º´æµ½RedisÖУ¬µ±Óû§ÇëÇóÖ¿ÓÑÁбíʱ£¬ÏÈ´ÓRedisÖжÁÈ¡Êý¾Ý£¬ÈôÊÇ»º´æÖв»±£´æ£¬Ôò´ÓÊý¾Ý¿âÖжÁÈ¡Êý¾Ý²¢»º´æµ½RedisÖС£ÕâÑù¿ÉÒÔ´ó´ó¼õÇáÊý¾Ý¿âµÄ¼ç¸º£¬²¢Ìá¸ß¶ÁÈ¡Êý¾ÝµÄЧÂÊ¡£
ʵÏֹ㲥ºÍ˽ÁĹ¦Ð§
ÔÚÉç½»Ó¦ÓÃÖУ¬¹ã²¥ºÍ˽ÁĹ¦Ð§Ò²ÊDZز»¿ÉÉٵġ£¹ã²¥¹¦Ð§¿ÉÒÔÈÃÐÂÎÅÒ»´ÎÐÔ·¢Ë͸øËùÓÐÔÚÏßÓû§£¬¶ø˽ÁĹ¦Ð§¿ÉÒÔʹÓû§Ö®¼ä¾ÙÐеã¶ÔµãµÄʵʱͨѶ¡£ÔÚSwooleÖУ¬¿ÉÒÔͨ¹ýWebSocketЧÀÍÆ÷À´ÊµÏÖÕâÁ½¸ö¹¦Ð§¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʵÏÖ·½·¨£º
$server->on('message', function (swoole_websocket_server $server, $frame) { $data = json_decode($frame->data, true); switch ($data['command']) { case 'broadcast': $server->push('broadcast', $data['message']); break; case 'private': $server->push($data['id'], $data['message']); break; } });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇͨ¹ýÅжÏÊÕµ½µÄÐÂÎŵÄÀàÐÍÀ´¾ÙÐй㲥»ò˽ÁIJÙ×÷¡£ÈôÊÇÊÕµ½µÄÐÂÎÅÀàÐÍÊÇbroadcast£¬Ôò½«ÐÂÎÅÍÆË͸øËùÓÐÔÚÏßÓû§£»ÈôÊÇÊÕµ½µÄÐÂÎÅÀàÐÍÊÇprivate£¬Ôò½«ÐÂÎÅÍÆË͸øÖ¸¶¨µÄÓû§¡£
ÔÚWebSocket¿Í»§¶ËÖУ¬ÎÒÃÇÒ²ÐèÒª×ö³öһЩÏìÓ¦µÄµ÷½â£¬ÀýÈçͨ¹ý¼ÓÈëbroadcast·¿¼äÀ´ÎüÊչ㲥ÐÂÎÅ£º
let ws = new WebSocket('ws://localhost:9501'); ws.onopen = function () { // ¼ÓÈëbroadcast·¿¼ä ws.send(JSON.stringify({command: 'join', room: 'broadcast'})); }; ws.onmessage = function (event) { let data = JSON.parse(event.data); // ´¦Öóͷ£¹ã²¥ÐÂÎÅ if (data.room === 'broadcast') { console.log(data.message); } };
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃWebSocket¿Í»§¶Ë¼ÓÈëbroadcast·¿¼ä£¬¿ÉÒÔÎüÊÕµ½Ð§ÀÍÆ÷¹ã²¥µÄÐÂÎÅ£¬²¢ÔÚ¿ØÖÆ̨ÖÐÊä³ö¡£
×ܽá
ͨ¹ýÒÔÉϵÄÑÝʾ£¬ÎÒÃÇ¿ÉÒÔ¿´µ½SwooleÌṩÁ˺ÜÊÇÇ¿Ê¢ºÍ¸»ºñµÄ¹¦Ð§£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏָ߲¢·¢¡¢µÍÑÓ³ÙµÄÉç½»¹¦Ð§¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇÐèҪƾ֤ÏêϸµÄÐèÇóºÍ³¡¾°À´Ñ¡ÔñÏìÓ¦µÄ¹¤¾ßºÍ¼Æ»®£¬ÒÔÌá¸ßÓû§ÌåÑéºÍϵͳµÄ¿Éά»¤ÐÔ¡£
ÒÔÉϾÍÊÇSwooleʵÏÖ¸ßÐÔÄܵÄÉç½»¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡