日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

laravel5.8 pusher socket.io

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 laravel5.8 pusher socket.io 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

用于掃碼狀態,廣播事件等簡單websocket服務,框架laravel5.8

pusher
  • 基于pusher外部服務,使用簡單,可以在任何框架中使用,laravel只是將發布廣播使用了事件觸發(具體觸發可以看laravel文檔),這里展示常規使用
  • 引入
  • composer require pusher/pusher-php-server

  • 服務端和客服端代碼分私有頻道和共有頻道,需要在pusher上注冊獲取秘鑰等
    2.1 共有頻道 注意channel和event請保持一致
  • $auth_key = 'xxxx';//pusher 提供的key$secret = 'xx';//pusher 提供$app_id = 'xxx';//pusher 提供app_id$option = ['cluster'=>'xx'];//pusher 提供的cluster$pusher = new Pusher\Pusher($auth_key, $secret, $app_id,$option);$response = $pusher->trigger('message','send-message','data:測試data'); <head> <meta name="csrf-token" content="{{ csrf_token() }}"> </head> <script src="https://js.pusher.com/7.0/pusher.min.js"></script> <script>Pusher.logToConsole = true;var pusher = new Pusher('pusher 提供的key', {cluster: 'pusher 提供的cluster'});var channel = pusher.subscribe('message');channel.bind('send-message', function(data) {alert(JSON.stringify(data));console.log(data);});</script>

    2.2 私有頻道

    //自己定義的認證方法 ,pusher 默認post請求。public function authorize(Request $request) {$auth_key = 'xxxx';//pusher 提供的key$secret = 'xx';//pusher 提供$app_id = 'xxx';//pusher 提供app_id$option = ['cluster'=>'xx'];//pusher 提供的cluster$pusher = new Pusher\Pusher($auth_key, $secret, $app_id,$option);echo $pusher->socket_auth($request->input('channel_name'),$request->input('socket_id'));//注意必須直接輸出,防止框架后續操作}public function sendmsg(){$auth_key = 'xxxx';//pusher 提供的key$secret = 'xx';//pusher 提供$app_id = 'xxx';//pusher 提供app_id$option = ['cluster'=>'xx'];//pusher 提供的cluster$pusher = new Pusher\Pusher($auth_key, $secret, $app_id,$option);$response = $pusher->trigger('private-chat1','send-message','data:測試data','soket_id 注意是阻止發送消息的soket_id');} <head> <meta name="csrf-token" content="{{ csrf_token() }}"> </head> <script src="https://js.pusher.com/7.0/pusher.min.js"></script> <script>var pusher = new Pusher('pusher 提供的key', {authEndpoint: 'http://mobile.app.com/api/channels/authorize',//自己定義的認證路由cluster: 'pusher 提供的cluster',encrypted: true,auth: {headers: {'X-CSRF-Token': this.csrfToken}}});channel = pusher.subscribe('private-chat1');//[private-] 相當于前綴,是必須的channel.bind('send-message', function(data) {alert(JSON.stringify(data));console.log(22);console.log(data);});</script>
    socket.io redis
  • 本地搭建socket服務,使用laravel的事件event觸發
  • 參考博文 https://blog.csdn.net/sym134/article/details/100569257

    這里記錄主要流程,注意順序不要錯了,不然3,4可能執行失敗
    2. 安裝所有依賴

    npm install

  • 首先我們需要全局安裝 laravel-echo-server
  • npm install -g laravel-echo-server

  • 安裝websocket客戶端
  • npm install --save socket.io-client

  • 安裝websocket客戶端封裝
  • npminstall --save laravel-echo

  • 監聽前端資源文件變化
  • npm run dev

  • 初始化服務配置
  • $ laravel-echo-server init

  • 啟動服務
  • laravel-echo-server start

  • 修改app.js resources/js/app.js
  • import Echo from 'laravel-echo'window.io = require('socket.io-client'); window.Echo = new Echo({broadcaster: 'socket.io',host: window.location.hostname + ':6001' });
  • 創建事件
  • php artisan make:event ExampleEvent

    <?phpnamespace App\Events;use Illuminate\Broadcasting\Channel; use Illuminate\Queue\SerializesModels; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast;class PushMsgEvent implements ShouldBroadcast {use Dispatchable, InteractsWithSockets, SerializesModels;public $msg;/*** Create a new event instance.** @return void*/public function __construct($msg){//$this->msg =$msg;}/*** Get the channels the event should broadcast on.** @return \Illuminate\Broadcasting\Channel|array*/public function broadcastOn(){return new Channel('user1');}public function broadcastWith(){return ['data' => $this->msg];} }
  • 啟用redis隊列
    配置自行處理.env[BROADCAST_DRIVER=redis,QUEUE_CONNECTION=redis]
  • php artisan queue:listen --tries=1

  • 前端 引入到頁面,我就是先拿首頁來測試了resources/views/welcome.blade.php
  • // 頭部加入 <meta name="csrf-token" content="{{ csrf_token() }}"> // 引入app.js <script src="js/app.js"></script> // 監聽并輸出 <script> // ’user1'是頻道,‘ExampleEvent’ 是監聽的事件(這是事件的類名,你也可以根據你的需要自定義)window.Echo.channel('user1').listen('PushMsgEvent', (e) => {console.log(e);}); </script>

    到這里廣播系統就完成了,如果需要私有頻道可以繼續閱讀
    14. 私有頻道

    總結

    以上是生活随笔為你收集整理的laravel5.8 pusher socket.io的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。