laravel+swoole之websocket消息推送

1.首先安裝 swoole 擴展 Swoole-1.x 須要 PHP-5.3.10 或更高版本 Swoole-2.x 須要 PHP-7.0.0 或更高版本javascript

公司環境是 php5.6.31因此比較麻煩須要編譯安裝,7以上直接使用命令 (pecl install swoole)php

wget https://github.com/swoole/swoole-src/archive/v1.10.1.tar.gz tar -zxvf v1.10.1.tar.gz cd swoole-src-1.10.1 phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make installhtml

而後在 php.ini 添加 swoole.so 擴展便可 2.使用 laravel 的 artisan 建立命令 php artisan make:command Swoole #建立一個命令 swoole 並會在 app/Console/Commands 增長一個 Swoole.php 的文件java

Commands\Swoole::Class #在 Kernel.php 裏增長命令列表 3.運行 socket 服務 1.編輯 app/Console/Command 裏的 Swoole.php 文件laravel

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

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 = 'swoole';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        switch ($action) {
            case 'close':

                break;

            default:
                $this->start();
                break;
        }

    }
    public function start()
    {
        //建立 websocket 服務器對象,監聽 0.0.0.0:9502 端口
        $this->ws = new \swoole_websocket_server("0.0.0.0", 9502);

        //監聽 WebSocket 鏈接打開事件
        $this->ws->on('open', function ($ws, $request) {
            var_dump($request->fd . "鏈接成功");
            // $ws->push($request->fd, "hello, welcome\n");
        });

        //監聽 WebSocket 消息事件
        $this->ws->on('message', function ($ws, $frame) {
            // echo "Message: {$frame->data}\n";
            // $ws->push($frame->fd, "server: {$frame->data}");
            // var_dump($ws->connection_info($frame->fd));
            //fd 綁定客戶端傳過來的標識 uid
            $ws->bind($frame->fd, $frame->data);
        });

        $this->ws->on('request', function ($request, $response) {
                // 接收 http 請求從 post 獲取參數
                // 獲取全部鏈接的客戶端,驗證 uid 給指定用戶推送消息
                // token 驗證推送來源,避免惡意訪問
                if ($request->post['token'] == ### ) {
                    $clients = $this->ws->getClientList();
                    $clientId = [];
                    foreach ($clients as $value) {
                        $clientInfo = $this->ws->connection_info($value);
                        if (array_key_exists('uid', $clientInfo) && $clientInfo['uid'] == $request->post['s_id']) {
                            $clientId[] = $value;
                        }
                    }
                    if (!empty($clientId)) {
                        foreach ($clientId as $v) {
                            $this->ws->push($v, $request->post['info']);
                        }
                    }
                }
            });

        //監聽 WebSocket 鏈接關閉事件
        $this->ws->on('close', function ($ws, $fd) {
            echo "client:{$fd} is closed\n";
        });

        $this->ws->start();
    }
}
【注】此處爲告終合 app 上傳數據時使用 curl 觸發 request 回調通知 web 端的實例因此使用了 httpserver 的 onrequest 事件,若是之後有更好的辦法去觸發服務端實時主動推送。
2.編輯 html

<div id="test">
    <a href="javascript:void(0)">運行 websocket</a>
</div>

$('#test').click(function(){
            if("WebSocket" in window){
                console.log("您的瀏覽器支持 websocket\n");
                var ws = new WebSocket("ws://66.66.66.66:9502");//建立 websocket 對象 
                ws.onopen = function(){
                    // ws.send("鏈接已創建\n");
                    ws.send($("#content").attr("js-sid"));
                    console.log("數據發送中");
                }

                ws.onmessage = function(evt){
                    var recv_msg = evt.data;
                    console.log("接受到的數據爲:"+recv_msg);
                }

                ws.onerror = function(evt,e){
                    console.log("錯誤信息爲"+e);
                }

                ws.onclose = function(){
                    console.log("鏈接已關閉");
                }

            }else{
                console.log("您的瀏覽器不支持 websocket\n");
            }
        });
3.curl 方法(調用就行)

public function swooletest($param = ['s_id'=>2, 'info'=>'info'])
    {
        $param['token'] = ###;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:9502");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        //設置 post 數據
        $post_data = $param;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_exec($ch);
        curl_close($ch);
    }
    
    
4.測試的時候直接在 laravel 的 artisan 所在目錄使用命令 php artisan swoole 便可啓動 socket 服務,而後頁面運行客戶端,最後調用 curl 推送數據。

4.成功以後
用 supervisor 守護 swoole 命令,或者 nohup 後臺啓動。

supervisor 配置麻煩不過能夠自動重啓,nohup 一條命令解決

nohup php artisan swoole &    #一條命令解決
此處說明幾個問題
1.此處我採用的是 bind 方法,當客戶端鏈接的時候 send 一個 uid 過來,而後在服務端處理的時候把 uid 和 fd 綁定在一塊兒,當你想向某個客戶端發送數據時傳一個 uid,經過 uid 找到 fd 進行指定發送,可是此處我用的是遍歷 getClientList 全部鏈接用戶(方法欠佳)的信息 connection_info 進行斷定。但願能改善這種方法

2.由於是 curl 訪問 httpserver 的形式,因此爲了不惡意訪問,加一個 token 驗證。

3.推送的信息轉換成 json 再傳,即 info 值

4.本實例的帳戶可能會在多個終端登陸,有多個 fd 綁定 uid,因此遍歷推送 push


複製代碼

閱讀原文請訪問如下連接 : www.zxb8.cc/?p=515git

相關文章
相關標籤/搜索