Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Echo ServerʵÏÖWebSocketsЧÀÍÆ÷£¿
Ëæ×ÅʵʱͨѶÊÖÒյĿìËÙÉú³¤£¬websocketsÒѳÉΪÐí¶àweb¿ª·¢Ö°Ô±µÄÑ¡Ôñ£¬laravel¿ò¼ÜÒ²²»ÆÆÀý¡£½èÖúlaravel echo server£¬web¿ª·¢Ö°Ô±¿ÉÒÔÇáËɵØʵÏÖwebsocketsЧÀÍÆ÷£¬¿ìËÙ¹¹½¨ÊµÊ±Í¨Ñ¶Ó¦ÓóÌÐò¡£±¾ÎĽ«Ìṩһ·ÝÏêϸµÄlaravel echo serverÈëÃÅÖ¸ÄÏ£¬×ÊÖúÄúÏàʶÔõÑùʹÓÃËüÔÚlaravelÓ¦ÓóÌÐòÖÐʵÏÖʵʱͨѶ¡£
ʲôÊÇLaravel Echo Server£¿
Laravel Echo ServerÊÇÒ»¸ö¿ªÔ´ÏîÄ¿£¬ËüÊÇLaravel EchoµÄʵʱͨѶЧÀÍÆ÷£¬¿ÉÒÔÓÃÓÚÓëLaravel EchoÅäºÏʹÓã¬ÊµÏÖWebSocketsͨѶ¡£Laravel EchoÊÇÒ»¸öJavaScript¿â£¬ËüÔÊÐí¿ª·¢Ö°Ô±Ê¹ÓüòÆÓµÄÓï·¨½«Ó¦ÓóÌÐòÅþÁ¬µ½WebSocketsЧÀÍÆ÷¡£¶øLaravel Echo Server¾ÍÊdz䵱ÁËЧÀÍÆ÷µÄ½ÇÉ«¡£
ÔÚLaravel Echo ServerÖУ¬¿ª·¢Ö°Ô±¿ÉÒÔʹÓÃNode.jsºÍSocket.io½¨ÉèʵʱͨѶЧÀÍÆ÷¡£Laravel Echo Server»ùÓÚSocket.io£¬ÌṩÁ˼òÆÓµÄAPIºÍ¿ØÖÆ̨£¬Àû±ãÓû§¹¹½¨WebSocketsЧÀÍÆ÷¡£
×°ÖÃLaravel Echo Server
Ê×ÏÈÐèҪװÖÃNode.jsºÍnpm¡£ÈôÊÇÄúÒѾװÖÃÁË£¬ÇëÌø¹ýÕâ¸ö°ì·¨¡£
×°ÖÃNode.jsºÍnpm
sudo apt-get update sudo apt-get install nodejs sudo apt-get install npm
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÇëÑéÖ¤ÊÇ·ñÀÖ³É×°ÖÃNode.jsºÍnpm¡£
node -v npm -v
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬Äú¿ÉÒÔʹÓÃnpm×°ÖÃLaravel Echo Server¡£ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁîÀ´×°Öãº
sudo npm install -g laravel-echo-server
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¼ì²éLaravel Echo ServerµÄ°æ±¾£º
laravel-echo-server -v
µÇ¼ºó¸´ÖÆ
ÉèÖÃLaravelÓ¦ÓóÌÐò
ÔÚʹÓÃLaravel Echo Server֮ǰ£¬ÄúÐèÒªÉèÖÃÄúµÄLaravelÓ¦ÓóÌÐò£¬ÒÔ±ãÓëWebSocketЧÀÍÆ÷ͨѶ¡£ÔÚLaravel 5.6°æ±¾Ö®ºó£¬Laravel¿ò¼ÜÄÚÖÃÁËPusherЧÀÍÌṩ³ÌÐò¡£ÔÚconfig/broadcasting.phpÖУ¬Äú¿ÉÒÔÕÒµ½PusherЧÀÍÌṩ³ÌÐòµÄÉèÖá£
ĬÈÏÇéÐÎÏ£¬Laravel¿ò¼ÜʹÓÃPusherЧÀÍÌṩ³ÌÐòÀ´´¦Àí¹ã²¥ÊÂÎñ¡£¿ÉÊÇ£¬ÈôÊÇÄúÏëʹÓÃLaravel Echo Server£¬ÄúÐèÒªÐ޸ĸÃÎļþ£¬ÒÔ±ãLaravel Echo¿ÉÒÔÔÚWebSocketЧÀÍÆ÷ÉϼàÌý¹ã²¥ÊÂÎñ¡£
ÔÚÐÞ¸Äconfig/broadcasting.phpÎļþ֮ǰ£¬ÄúÐèÒªÏÈ×°ÖÃcomposerÒÀÀµÏî¡£
composer require predis/predis
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬·¿ªconfig/broadcasting.phpÎļþ£¬²¢½«¹ã²¥Çý¶¯³ÌÐò¸ü¸ÄΪredis¡£
'connections' => [ 'redis' => [ 'driver' => 'redis', 'connection' => 'default', ], ],
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÄúÐèҪװÖÃRedisЧÀͲ¢Æô¶¯Ëü¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî×°ÖÃRedis£º
sudo apt-get install -y redis-server
µÇ¼ºó¸´ÖÆ
Æô¶¯redisЧÀÍ£º
sudo systemctl start redis
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬LaravelÓ¦ÓóÌÐòÒÑ×¼±¸ºÃÓëLaravel Echo ServerͨѶ¡£
ÉèÖÃLaravel Echo Server
ÔÚ±¾½ÚÖУ¬ÎÒÃǽ«ÉèÖÃLaravel Echo Server²¢Æô¶¯Ëü¡£Laravel Echo ServerÌṩÁËÒ»¸ö¼òÆÓµÄÉèÖÃÎļþ£¬Äú¿ÉÒÔͨ¹ý¸ü¸Ä¸ÃÎļþµÄÉèÖÃÀ´ÉèÖÃWebSocketЧÀÍÆ÷¡£Ä¬ÈÏÇéÐÎÏ£¬Laravel Echo ServerʹÓÃÎļþ´æ´¢À´ÉúÑÄ¿Í»§¶ËÊý¾Ý£¬¿ÉÊÇÄú¿ÉÒÔÑ¡ÔñʹÓÃRedisÀ´´æ´¢¿Í»§¶ËÊý¾Ý¡£
ÉèÖÃLaravel Echo ServerÎļþ
ÔÚʹÓÃLaravel Echo Server֮ǰ£¬ÄúÐèÒªÏȽ¨ÉèÒ»¸öÉèÖÃÎļþ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÔÚÓ¦ÓóÌÐòµÄ¸ùĿ¼Öн¨ÉèÉèÖÃÎļþ¡£
laravel-echo-server init
µÇ¼ºó¸´ÖÆ
ÔËÐÐÍêinitÏÂÁîºó£¬Laravel Echo Server½«½¨ÉèÒ»¸öеÄlaravel-echo-server.jsonÉèÖÃÎļþ¡£Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´±à¼ÉèÖÃÎļþ¡£
nano laravel-echo-server.json
µÇ¼ºó¸´ÖÆ
ÔÚ±à¼Æ÷ÖУ¬Äú½«¿´µ½ÉèÖÃÎļþµÄĬÈÏÉèÖá£ÒÔÏÂÊÇһЩ³£¼ûµÄÉèÖã¬Äú¿ÉÒÔƾ֤ÐèÒª¸ü¸ÄËüÃÇ¡£
{ "authHost": "http://localhost", "authEndpoint": "/broadcasting/auth", "clients": [], "database": "redis", "databaseConfig": { "redis": {}, "sqlite": { "databasePath": "/database/laravel-echo-server.sqlite" } }, "devMode": false, "host": null, "port": "6001", "protocol": "http", "socketio": {}, "sslCertPath": "", "sslKeyPath": "", "sslCertChainPath": "", "sslPassphrase": "", "subscribers": { "http": true, "redis": true }, "apiOriginAllow": { "allowCors": false, "allowOrigin": "", "allowMethods": "", "allowHeaders": "" } }
µÇ¼ºó¸´ÖÆ
authHost£ºÔÚLaravelÖÐÉèÖõÄÓ¦ÓóÌÐòÖ÷»úÃû³Æ/µØµã¡£
authEndpoint£ºLaravelÓ¦ÓóÌÐòÖеĹ㲥Éí·ÝÑéÖ¤¶Ëµã¡£
clients£º¿Í»§¶ËÃû³ÆºÍÃÜÔ¿¡£
database£º¿Í»§¶ËÊý¾Ý´æ´¢ÀàÐÍ¡£
devMode£ºLaravel Echo ServerÊÇ·ñÔËÐÐÔÚ¿ª·¢Ä£Ê½Ï¡£
host£ºWebSocketЧÀÍÆ÷°ó¶¨µÄÖ÷»úÃû¡£
port£ºWebSocketЧÀÍÆ÷°ó¶¨µÄ¶Ë¿Ú¡£
protocol£ºWebSocketЧÀÍÆ÷µÄÐÒé¡£
subscribers£ºÐÂÎŶ©ÔÄÕß¡£
apiOriginAllow£ºÖ§³ÖAPIÇëÇóµÄÖ÷»ú¡£
ƾ֤ÄúµÄÏÖÕæÏàÐΣ¬¸ü¸Ä²¢ÉúÑÄÉèÖÃÎļþ¡£
Æô¶¯Laravel Echo Server
ÔÚÍê³ÉÉèÖúó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÆô¶¯Laravel Echo Server¡£
laravel-echo-server start
µÇ¼ºó¸´ÖÆ
Laravel Echo ServerÏÖÔÚÒÑÀÖ³ÉÆô¶¯£¬²¢ÇÒ¿ÉÒÔÓÃÓÚʵÏÖWebSocketЧÀÍÆ÷¡£
ʹÓÃLaravel Echo ServerʵÏÖʵʱͨѶÊǺÜÊÇÈÝÒ׵ġ£ÔÚLaravelÖУ¬Ö»ÐèʹÓÃÒÔÏÂÏÂÁî¹ã²¥ÊÂÎñ£º
broadcast(new WebsocketDemoEvent($user, $message));
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚLaravel Echo ServerÉϹ㲥һ¸öеÄWebsocketDemoEventÊÂÎñ¡£ÇëÈ·±£ÄúÒÑÔÚ¿ØÖÆ̨ÖÐʹÓÃÒÔÉÏÏÂÁîÆô¶¯Laravel Echo Server¡£
Laravel Echo ServerÌṩÁËÒ»¸öÓѺõÄÖÎÀí½çÃ棬Äú¿ÉÒÔͨ¹ýä¯ÀÀÆ÷»á¼û¸Ã½çÃæÒÔÉó²éWebSocketЧÀÍÆ÷µÄ״̬ÐÅÏ¢¡£Ä¬ÈÏÇéÐÎÏ£¬¸ÃÖÎÀí½çÃæλÓÚhttp://localhost:6001¡£
µ½ÕâÀ±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃLaravel Echo ServerʵÏÖWebSocketsЧÀÍÆ÷¡£Laravel Echo ServerÌṩÁËÒ»¸öÇáÓ¯µÄÒªÁìÀ´´¦ÀíʵʱͨѶ£¬ÕâʹµÃWebSockets¶Ô¿í´óWeb¿ª·¢Ö°Ô±ºÜÊÇÓÐÓá£ÈôÊÇÄúÕýÔÚÑ°ÕÒÒ»ÖÖÈÝÒ×ʵÏÖʵʱͨѶµÄÒªÁ죬¾ÍÎÞ·ÁʵÑéÒ»ÏÂLaravel Echo Server°É¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Echo ServerʵÏÖWebSocketsЧÀÍÆ÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡