實現websocket 主動消息推送,laravel+swoole

實現一個可以主動觸發消息推送的功能,這個可以實現向模板消息那個,給予所有成員發送自定義消息,而不需要通過客戶端發送消息,服務端上message中監聽傳送的消息進行做相對於的業務邏輯。

主動消息推送實現
平常我們採用 swoole 來寫 WebSocket 服務可能最多的用到的是open,message,close這三個監聽狀態,但是萬萬沒有看下下面的onRequest回調的使用,沒錯,解決這次主動消息推送的就是需要用onRequest回調。
官方文檔:正因爲swoole_websocket_server繼承自swoole_http_server,所以在 websocket 中有onRequest回調。

詳細實現:

# 這裏是一個laravel中Commands
# 運行php artisan swoole start 即可運行
<?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;
        }
    }

    /**
     * 啓動Swoole
     */
    private function start()
    {
        $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
        //監聽WebSocket連接打開事件
        $this->ws->on('open', function ($ws, $request) {
        });
        //監聽WebSocket消息事件
        $this->ws->on('message', function ($ws, $frame) {
            $this->info("client is SendMessage\n");
        });
        //監聽WebSocket主動推送消息事件
        $this->ws->on('request', function ($request, $response) {
            $scene = $request->post['scene'];       // 獲取值
            $this->info("client is PushMessage\n".$scene);
        });
        //監聽WebSocket連接關閉事件
        $this->ws->on('close', function ($ws, $fd) {
            $this->info("client is close\n");
        });
        $this->ws->start();
    }
}

前面說的是 swoole 中onRequest的實現,下面實現下在控制器中主動觸發onRequest回調。實現方法就是我們熟悉的curl請求。

# 調用activepush方法以後,會在cmd中打印出 
# client is PushMessage 主動推送消息 字眼
    /**
     * CURL請求
     * @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);
    }
    
    /**
     * 主動觸發
     */
    public function activepush()
    {
        $param['scene'] = '主動推送消息';
        $this->curl($param);            // 主動推送消息

用途
onRequest 回調特別適用於需要在控制器中調用的推送消息,比如模板消息之類,在控制器中調用。

大部分phper在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那裏入手去提升,對此我整理了一些資料,包括但不限於:分佈式架構、高可擴展、高性能、高併發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階乾貨需要的可以免費分享給大家,需要請戳這裏
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章