SwooleÓëRedisµÄÕûºÏ£º¿ìËÙ¹¹½¨¸ß¿ÉÓÃÐÔIMϵͳ
Ëæ×ÅÒƶ¯»¥ÁªÍøµÄÐËÆ𣬼´Ê±Í¨Ñ¶£¨im£©ÏµÍ³ÒѳÉΪÎÒÃÇÒ»Ñùƽ³£ÉúÑÄÖв»¿É»òȱµÄÒ»²¿·Ö¡£Òò´Ë£¬¿ª·¢Ò»¿îÊÊÓᢸßÐÔÄܵÄimϵͳÒѳÉΪÐí¶à¿ª·¢Õß×·ÇóµÄÄ¿µÄÖ®Ò»¡£ÔÚÕâ¸öÀú³ÌÖУ¬swoole ºÍ redis ÊÇÁ½ÖÖ³£ÓõÄÐÔÄܹ¤¾ß£¬ËüÃÇ¿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËÙ¹¹½¨¸ß¿ÉÓÃÐÔµÄimϵͳ¡£
±¾ÎĽ«ÏÈÈÝ Swoole ºÍ Redis µÄ»ù±¾¿´·¨ºÍÔÀí£¬²¢¸ø³öÒ»¸ö»ùÓÚÕâÁ½ÖÖ¹¤¾ßµÄIMϵͳ¼Ü¹¹°¸ÀýºÍʵÏּƻ®¡£Ï£Íûͨ¹ý±¾ÎÄ£¬¶ÁÕßÄܹ»ÏàʶÔõÑùʹÓÃÕâÁ½ÖÖ¹¤¾ß´î½¨¸ßÐÔÄܵÄIMϵͳ¡£
Ò»¡¢ SwooleºÍRedis¸ÅÊö
1.1 Swoole
SwooleÊÇÒ»¿î»ùÓÚ PHP µÄ¸ß¼¶ÍøÂç¿ò¼Ü£¬ËüÊÇÈ«Òì²½¡¢¸ßÐÔÄܵÄЧÀÍÆ÷¶ËÒýÇ棬֧³Ö TCP¡¢UDP¡¢WebSocket ÒÔ¼° HTTP ÐÒé¡£Swoole Äܹ»´ó´óÌá¸ß PHP Àú³ÌµÄÐÔÄܺͲ¢·¢¶È£¬Ö÷ÒªÔµ¹ÊÔÓÉÊÇËüµÄ»ùÓÚ epoll »ò kqueue µÄ reacto »úÖÆ£¬½ÓÄÉÁËгÌÊÖÒÕʵÏÖÁË·ÇÛÕ±ÕµÄÒì²½±à³Ì¡£
1.2 Redis
RedisÊÇÒ»¿î¿ªÔ´µÄÄÚ´æ¼üÖµÊý¾Ý¿â£¬ËüÖ§³Ö¶àÖÖÊý¾Ý½á¹¹£¬°üÀ¨×Ö·û´®¡¢¹þÏ£¡¢ÁÐ±í¡¢ÜöÝͺÍÓÐÐòÜöÝ͵ȡ£RedisÓкܸߵÄÐÔÄܺͿÉÀ©Õ¹ÐÔ£¬¿ÉÒÔ´¦ÀíÊý°ÙÍò¼¶±ðµÄ²¢·¢ÇëÇó¡£Redis Ö÷ÒªÓÃÓÚ»º´æ¡¢ÐÂÎÅÐÐÁС¢ÂþÑÜʽËøµÈ³¡¾°¡£
¶þ¡¢ SwooleºÍRedisÕûºÏ
2.1 SwooleÔõÑùÓëRedisÐͬÊÂÇé
ÔÚ Swoole ÖÐʹÓà Redis ͨ³£ÓÐÒÔÏÂÁ½ÖÖ·½·¨£º
ʹÓà Swoole Redis ¿Í»§¶Ë
ʹÓà Swoole Coroutine Redis ¿Í»§¶Ë
ÆäÖУ¬Swoole Redis ¿Í»§¶ËÊÇÒ»¸ö¹Å°åµÄ Redis ¿Í»§¶Ë£¬ÐèҪʹÓûص÷º¯ÊýÀ´´¦ÀíÇëÇóÏìÓ¦£»¶ø Coroutine Redis ¿Í»§¶ËÔòͨ¹ýг̵ķ½·¨´¦ÀíÇëÇóºÍÏìÓ¦£¬Ê¹ÓÃÆðÀ´¸üΪÀû±ãºÍ¸ßЧ¡£
2.2 ¼Ü¹¹ËµÃ÷
ΪÁËʵÏÖʵʱÐÔµÄÏàͬ£¬IMϵͳͨ³£½ÓÄÉ WebSocket ÐÒéÀ´´«ÊäÐÂÎÅ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«¹¹½¨Ò»¸ö»ùÓÚ Swoole ºÍ Redis µÄ WebSocket ЧÀÍÆ÷£¬¿Í»§¶Ë·¢Ë͵ÄÐÂÎŽ«»á±»ÉúÑĵ½ Redis ÖУ¬È»ºóͨ¹ýЧÀÍÆ÷ÍÆË͸øÆäËû¿Í»§¶Ë¡£
¿Í»§¶Ë·¢ËÍÐÂÎŵ½ WebSocket ЧÀÍÆ÷
WebSocket ЧÀÍÆ÷½«ÐÂÎÅÉúÑĵ½ Redis ÖÐ
Redis ÍÆËÍÐÂÎŵ½Ð§ÀÍÆ÷
WebSocket ЧÀÍÆ÷½«ÐÂÎÅÍÆË͵½ÆäËû¿Í»§¶Ë
Èý¡¢ÊµÏּƻ®
½ÓÏÂÀ´£¬ÎÒÃǽ«Õë¶Ôÿ¸ö°ì·¨ÏêϸÏÈÈÝʵÏּƻ®¡£
3.1 ЧÀͶ˴úÂë
£¨1£©Æô¶¯ WebSocket ЧÀÍÆ÷
ʹÓà Swoole ÌṩµÄ WebSocket ЧÀÍÆ÷ API À´Æô¶¯Ð§ÀÍÆ÷£¬´úÂëÈçÏ£º
$server = new SwooleWebSocketServer("0.0.0.0", 9501); $server->on('open', function (SwooleWebSocketServer $server, $frame) { echo "connection open "; }); $server->on('message', function (SwooleWebSocketServer $server, $frame) { $redis = new SwooleCoroutineRedis(); $redis->connect('127.0.0.1', 6379); $redis->lPush('messages', $frame->data); }); $server->on('close', function (SwooleWebSocketServer $server, $fd) { echo "connection close "; }); $server->start();
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÖУ¬ÎÒÃÇʹÓà $server->on() º¯ÊýÀ´ÉèÖà WebSocket µÄ open¡¢message ºÍ close ÊÂÎñ»Øµ÷º¯Êý¡£µ±¿Í»§¶ËÅþÁ¬µ½Ð§ÀÍÆ÷ʱ£¬»áÖ´ÐÐ open º¯ÊýÖеĴúÂ룻µ±¿Í»§¶ËÏòЧÀÍÆ÷·¢ËÍÐÂÎÅʱ£¬»áÖ´ÐÐ message º¯ÊýÖеĴúÂë¡£ÔÚ message º¯ÊýÖУ¬ÎÒÃǽ¨ÉèÒ»¸ö Coroutine Redis ¿Í»§¶Ë£¬²¢½«¿Í»§¶Ë·¢Ë͵ÄÐÂÎÅ»º´æµ½ Redis ÐÐÁÐÖС£
£¨2£©ÍÆËÍÐÂΟø¿Í»§¶Ë
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪʵÏÖЧÀÍÆ÷ÍÆËÍÐÂΟø¿Í»§¶ËµÄÂß¼¡£ÕâÀï¿ÉÒÔʹÓà Swoole ÌṩµÄ push() º¯ÊýÀ´ÊµÏÖ£¬´úÂëÈçÏ£º
// ÍÆËÍÐÂΟø¿Í»§¶Ë $server->tick(1000, function () use ($server) { $redis = new SwooleCoroutineRedis(); $redis->connect('127.0.0.1', 6379); while ($message = $redis->rPop('messages')) { foreach ($server->connections as $fd) { $server->push($fd, $message); } } });
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÖУ¬ÎÒÃÇʹÓà Swoole ÌṩµÄ tick() º¯ÊýÀ´×¼Ê±Ö´ÐдúÂ룬ʹÓà Coroutine Redis ¿Í»§¶Ë´Ó Redis ÖÐÈ¡³öÐÂÎÅ£¬²¢½«ÐÂÎÅÍÆË͸øËùÓпͻ§¶Ë¡£
3.2 ¿Í»§¶Ë´úÂë
¿Í»§¶Ë´úÂë½ÏÁ¿¼òÆÓ£¬ÎÒÃÇÖ»ÐèҪʹÓà WebSocket ¿Í»§¶ËÅþÁ¬ WebSocket ЧÀÍÆ÷£¬²¢Í¨¹ý JavaScript À´·¢ËͺÍÎüÊÕÊý¾Ý¾Í¿ÉÒÔÁË¡£´úÂëÈçÏ£º
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>IM System</title> </head> <body> <h1>IM System</h1> <form> <label for="name">Name:</label> <input type="text" id="name"> <br><br> <label for="message">Message:</label> <input type="text" id="message"> <br><br> <input type="submit" value="Send Message"> </form> <br><br> <div id="message-list"></div> <script> var socket = new WebSocket('ws://localhost:9501'); socket.onopen = function(event) { console.log('WebSocket connect succeed'); }; socket.onmessage = function(event) { var message = JSON.parse(event.data); var messageList = document.getElementById('message-list'); var p = document.createElement('p'); p.innerText = message.name + ": " + message.message; messageList.prepend(p); }; document.querySelector('form').addEventListener('submit', function(event) { event.preventDefault(); var name = document.getElementById('name').value; var message = document.getElementById('message').value; socket.send(JSON.stringify({ name: name, message: message })); document.getElementById('message').value = ""; }); </script> </body> </html>
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓà WebSocket ¿Í»§¶ËÅþÁ¬ WebSocket ЧÀÍÆ÷¡£µ±¿Í»§¶ËÅþÁ¬Àֳɺó£¬ÎÒÃǾͿÉÒÔͨ¹ý JavaScript ÖÐµÄ WebSocket ¹¤¾ßµÄ send() ÒªÁìÀ´·¢ËÍÐÂΟøЧÀÍÆ÷£¬Í¬Ê±»¹ÐèÒªÉèÖà onmessage »Øµ÷º¯ÊýÀ´ÎüÊÕЧÀÍÆ÷ÍÆË͵ÄÐÂÎÅ¡£
ËÄ¡¢×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁË Swoole ºÍ Redis µÄ»ù±¾¿´·¨ºÍÔÀí£¬²¢¹²ÏíÁËÒ»¸ö»ùÓÚ Swoole ºÍ Redis µÄ WebSocket ЧÀÍÆ÷¼Ü¹¹°¸ÀýºÍʵÏּƻ®¡£Í¨¹ýÕâ¸ö°¸Àý£¬ÎÒÃÇ¿ÉÒÔÏàʶµ½ Swoole ºÍ Redis ÔõÑùÐͬÊÂÇ飬¹¹½¨¸ßÐÔÄÜ¡¢¸ß¿ÉÓÃµÄ IM ϵͳ¡£
ËäÈ»£¬ÕâÖ»ÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ÏÖʵÖл¹ÐèҪ˼Á¿Ðí¶à·½Ã棬ÈçÇå¾²ÐÔ¡¢ÐÔÄÜÓÅ»¯µÈ¡£Ï£Íû¶ÁÕß¿ÉÒÔͨ¹ý±¾ÎÄÏàʶµ½ÕâÁ½¸ö¹¤¾ßµÄʹÓã¬Í¬Ê±Ò²Ï£Íû¶ÁÕßÄܹ»¼ÌÐøÉîÈëÑо¿ÕâÁ½¸ö¹¤¾ßºÍÆäËûÏà¹ØÊÖÒÕ£¬Îª¿ª·¢¸ßÐÔÄܵÄÓ¦ÓÃ×ö³ö¸ü¶àµÄТ˳¡£
ÒÔÉϾÍÊÇSwooleÓëRedisµÄÕûºÏ£º¿ìËÙ¹¹½¨¸ß¿ÉÓÃÐÔIMϵͳµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡