×ðÁú¿­Ê±

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ϵͳµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

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

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

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