×ðÁú¿­Ê±¹ÙÍøµÇ¼

ThinkPHP6ÖÐÔõÑùʹÓÃWebSocket¾ÙÐÐʵʱͨѶ£¿

websocketÊÇÒ»ÖÖÈ«Ë«¹¤Í¨Ñ¶Ð­Ò飬Äܹ»ÔÚЧÀÍÆ÷ºÍ¿Í»§¶ËÖ®¼ä½¨ÉèʵʱÅþÁ¬£¬ÒÔʵÏÖʵʱͨѶ¡£ÔÚweb¿ª·¢ÖУ¬³£ÓõÄphp¿ò¼ÜÓÐthinkphp£¬ÄÇôÔÚthinkphp6ÖÐÔõÑùʹÓÃwebsocket¾ÙÐÐʵʱͨѶÄØ£¿

×°ÖÃswooleÀ©Õ¹

Ê×ÏÈÐèÒªÔÚЧÀÍÆ÷ÉÏ×°ÖÃswooleÀ©Õ¹£¬¿ÉʹÓÃcomposerÏÂÁî¾ÙÐÐ×°Öãº

composer require swoole/swoole

µÇ¼ºó¸´ÖÆ

×¢ÖØ£ºÊ¹ÓÃswooleÀ©Õ¹ÐèÒªPHP°æ±¾>=7.0¡£

½¨ÉèWebSocketЧÀÍ

ÔÚThinkPHP6ÖУ¬¿ÉÒÔͨ¹ý×Ô½ç˵ÏÂÁÉèWebSocketЧÀÍ¡£·­¿ªÏÂÁîÐй¤¾ß£¬½øÈëÏîÄ¿¸ùĿ¼£¬Ö´ÐÐÈçÏÂÏÂÁ

php think make:command WebSocket

µÇ¼ºó¸´ÖÆ

Ö´ÐÐÍêÏÂÁîºó£¬»áÔÚapp/commandĿ¼ÏÂÌìÉúWebSocket.phpÎļþ¡£ÔÚ¸ÃÎļþÖУ¬Ìí¼ÓÒÔÏ´úÂ룺

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

<?php
namespace appcommand;

use swoole_websocket_server;
use swoole_http_request;
use thinkconsoleCommand;
use thinkconsoleInput;
use thinkconsoleOutput;

class WebSocket extends Command
{
    protected function configure()
    {
        // ¸øÏÂÁîÆðÒ»¸öÃû×Ö
        $this->setName('swoole:websocket')
            ->setDescription('Start websocket server');
    }

    protected function execute(Input $input, Output $output)
    {
        $server = new swoole_websocket_server("0.0.0.0", 9501);

        // ¼àÌýWebSocketÅþÁ¬·­¿ªÊÂÎñ
        $server->on('open', function (swoole_websocket_server $server, swoole_http_request $request) {
            echo "connection open: {$request->fd}
";
        });

        // ¼àÌýWebSocketÐÂÎÅÊÂÎñ
        $server->on('message', function (swoole_websocket_server $server, $frame) {
            echo "received message: {$frame->data}
";

            // ¹ã²¥ÐÂΟøËùÓÐÅþÁ¬µÄ¿Í»§¶Ë
            $server->push($frame->fd, "this is server");
        });

        // ¼àÌýWebSocketÅþÁ¬¹Ø±ÕÊÂÎñ
        $server->on('close', function ($ser, $fd) {
            echo "connection close: {$fd}
";
        });

        $server->start();
    }
}

µÇ¼ºó¸´ÖÆ

Ö´ÐÐÈçÏÂÏÂÁ¼´¿ÉÆô¶¯WebSocketЧÀÍ£º

php think swoole:websocket

µÇ¼ºó¸´ÖÆ

ÔÚÊÓͼÖÐʹÓÃWebSocket

ÔÚÊÓͼÖУ¬¿ÉÒÔʹÓÃJavaScriptµÄWebSocket APIÓëЧÀͶ˾ÙÐÐʵʱͨѶ¡£ÀýÈ磺

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
</head>
<body>
    <script>
        var ws = new WebSocket('ws://localhost:9501');

        ws.onopen = function(){
            console.log('WebSocket open');
        };

        ws.onmessage = function(ev){
            console.log('WebSocket message: ' + ev.data);
        };

        ws.onclose = function(){
            console.log('WebSocket close');
        };
    </script>
</body>
</html>

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂ뽨ÉèÁËÒ»¸öWebSocketʵÀý£¬ÅþÁ¬µ½ÍâµØWebSocketЧÀÍ¡£µ±Ð§ÀͶ˷¢À´ÐÂÎÅʱ£¬Å²ÓÃonmessageº¯Êý¾ÙÐд¦Àí¡£¿ÉÒÔͨ¹ýŲÓÃʵÀýµÄsendº¯ÊýÏòЧÀͶ˷¢ËÍÐÂÎÅ¡£

ÖÁ´Ë£¬WebSocketЧÀÍÒѾ­Àֳɽ¨Éè²¢ÓëÇ°¶Ë½¨ÉèʵʱͨѶÅþÁ¬¡£

×ܽá

ÔÚThinkPHP6ÖУ¬½èÖúswooleÀ©Õ¹£¬¿ÉÒÔÇáËÉʵÏÖWebSocketʵʱͨѶ¹¦Ð§¡£Í¨¹ý×Ô½ç˵ÏÂÁÆôWebSocketЧÀÍ£¬ÔÙÍŽáJavaScript WebSocket API£¬¼´¿ÉÔÚWebÓ¦ÓÃÖÐʵÏÖʵʱͨѶ£¬Öª×ã¶àÖÖÓªÒµÐèÇó¡£

ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʹÓÃWebSocket¾ÙÐÐʵʱͨѶ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ