Laravel使用swoole實(shí)現(xiàn)websocket主動(dòng)消息推送的方法介紹
需求
需要實(shí)現(xiàn)一個(gè)可以主動(dòng)觸發(fā)消息推送的功能,這個(gè)可以實(shí)現(xiàn)向模板消息那個(gè),給予所有成員發(fā)送自定義消息,而不需要通過客戶端發(fā)送消息,服務(wù)端上message中監(jiān)聽傳送的消息進(jìn)行做相對(duì)于的業(yè)務(wù)邏輯。
主動(dòng)消息推送實(shí)現(xiàn)
平常我們采用 swoole 來(lái)寫 WebSocket 服務(wù)可能最多的用到的是open,message,close這三個(gè)監(jiān)聽狀態(tài),但是萬(wàn)萬(wàn)沒有看下下面的onRequest回調(diào)的使用,沒錯(cuò),解決這次主動(dòng)消息推送的就是需要用onRequest回調(diào)。
官方文檔:正因?yàn)閟woole_websocket_server繼承自swoole_http_server,所以在 websocket 中有onRequest回調(diào)。
詳細(xì)實(shí)現(xiàn):
# 這里是一個(gè)laravel中Commands # 運(yùn)行php artisan swoole start 即可運(yùn)行 <?php namespace App\Console\Commands; use Illuminate\Console\Command; use swoole_websocket_server; class Swoole extends Command { public $ws; /** * The name and signature of the console command. * * @var string */ protected $signature = 'swoole {action}'; /** * The console command description. * * @var string */ protected $description = 'Active Push Message'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $arg = $this->argument('action'); switch ($arg) { case 'start': $this->info('swoole server started'); $this->start(); break; case 'stop': $this->info('swoole server stoped'); break; case 'restart': $this->info('swoole server restarted'); break; } } /** * 啟動(dòng)Swoole */ private function start() { $this->ws = new swoole_websocket_server("0.0.0.0", 9502); //監(jiān)聽WebSocket連接打開事件 $this->ws->on('open', function ($ws, $request) { }); //監(jiān)聽WebSocket消息事件 $this->ws->on('message', function ($ws, $frame) { $this->info("client is SendMessage\n"); }); //監(jiān)聽WebSocket主動(dòng)推送消息事件 $this->ws->on('request', function ($request, $response) { $scene = $request->post['scene']; // 獲取值 $this->info("client is PushMessage\n".$scene); }); //監(jiān)聽WebSocket連接關(guān)閉事件 $this->ws->on('close', function ($ws, $fd) { $this->info("client is close\n"); }); $this->ws->start(); } }
前面說(shuō)的是 swoole 中onRequest的實(shí)現(xiàn),下面實(shí)現(xiàn)下在控制器中主動(dòng)觸發(fā)onRequest回調(diào)。實(shí)現(xiàn)方法就是我們熟悉的curl請(qǐng)求。
# 調(diào)用activepush方法以后,會(huì)在cmd中打印出 # client is PushMessage 主動(dòng)推送消息 字眼 /** * CURL請(qǐng)求 * @param $data */ public function curl($data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://127.0.0.1:9502"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_exec($curl); curl_close($curl); } /** * 主動(dòng)觸發(fā) */ public function activepush() { $param['scene'] = '主動(dòng)推送消息'; $this->curl($param); // 主動(dòng)推送消息
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
PHP與Java進(jìn)行通信的實(shí)現(xiàn)方法
這篇文章主要介紹了php與java通信的實(shí)現(xiàn)方法,需要的朋友可以參考下2013-10-10YII CLinkPager分頁(yè)類擴(kuò)展增加顯示共多少頁(yè)
yii的分頁(yè)類CLinkPager默認(rèn)是不支持顯示共x頁(yè)的,那么現(xiàn)在接的項(xiàng)目有這樣的需求,怎么辦呢?下面通過本文給大家介紹YII CLinkPager分頁(yè)類擴(kuò)展增加顯示共多少頁(yè)的實(shí)例代碼,需要的朋友參考下吧2016-01-01Smarty結(jié)合Ajax實(shí)現(xiàn)無(wú)刷新留言本實(shí)例
Smarty結(jié)合Ajax實(shí)現(xiàn)無(wú)刷新留言本實(shí)例...2007-01-01PHP和javascript常用正則表達(dá)式及用法實(shí)例
這篇文章主要介紹了常用的PHP和javascript正則表達(dá)式及用法實(shí)例,精心收集的PHP和javascript正則表達(dá)式各10個(gè),需要的朋友可以參考下2014-07-07laravel 關(guān)聯(lián)關(guān)系遍歷數(shù)組的例子
今天小編就為大家分享一篇laravel 關(guān)聯(lián)關(guān)系遍歷數(shù)組的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-10-10