首頁  >  文章  >  後端開發  >  用laravel+Swoole實作websocket主動訊息推送

用laravel+Swoole實作websocket主動訊息推送

angryTom
angryTom轉載
2019-11-29 16:36:253543瀏覽

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

用laravel+Swoole實作websocket主動訊息推送

主動訊息推送實作

#平常我們採用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 = &#39;swoole {action}&#39;;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;Active Push Message&#39;;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $arg = $this->argument(&#39;action&#39;);
        switch ($arg) {
            case &#39;start&#39;:
                $this->info(&#39;swoole server started&#39;);
                $this->start();
                break;
            case &#39;stop&#39;:
                $this->info(&#39;swoole server stoped&#39;);
                break;
            case &#39;restart&#39;:
                $this->info(&#39;swoole server restarted&#39;);
                break;
        }
    }

    /**
     * 启动Swoole
     */
    private function start()
    {
        $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
        //监听WebSocket连接打开事件
        $this->ws->on(&#39;open&#39;, function ($ws, $request) {
        });
        //监听WebSocket消息事件
        $this->ws->on(&#39;message&#39;, function ($ws, $frame) {
            $this->info("client is SendMessage\n");
        });
        //监听WebSocket主动推送消息事件
        $this->ws->on(&#39;request&#39;, function ($request, $response) {
            $scene = $request->post[&#39;scene&#39;];       // 获取值
            $this->info("client is PushMessage\n".$scene);
        });
        //监听WebSocket连接关闭事件
        $this->ws->on(&#39;close&#39;, 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[&#39;scene&#39;] = &#39;主动推送消息&#39;;
        $this->curl($param);            // 主动推送消息

用途
onRequest 回呼特別適用於需要在控制器中呼叫的推播訊息,例如範本訊息之類,在控制器中呼叫。

更多PHP相關知識,請造訪PHP中文網

以上是用laravel+Swoole實作websocket主動訊息推送的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除