swoole.php 2.39 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4
<?php

namespace App\Console\Commands;

冯超鹏's avatar
冯超鹏 committed
5
use App\Http\Controllers\Auth\SwooleCommandController;
冯超鹏's avatar
冯超鹏 committed
6
use Illuminate\Console\Command;
冯超鹏's avatar
冯超鹏 committed
7
use Illuminate\Support\Facades\Log;
冯超鹏's avatar
冯超鹏 committed
8 9
class swoole extends Command
{
冯超鹏's avatar
冯超鹏 committed
10 11
    public $ws;
    private $token = 'zxc123456789ZXC';
12
    public $time = 10;
冯超鹏's avatar
冯超鹏 committed
13 14 15 16 17
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
冯超鹏's avatar
冯超鹏 committed
18
    protected $signature = 'swoole {action?}';
冯超鹏's avatar
冯超鹏 committed
19 20 21 22 23 24

    /**
     * The console command description.
     *
     * @var string
     */
冯超鹏's avatar
冯超鹏 committed
25
    protected $description = 'swoole';
冯超鹏's avatar
冯超鹏 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
冯超鹏's avatar
冯超鹏 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        $action = $this->argument('action');
        switch ($action) {
            case 'close':
                break;
            default:
                $this->start();
                break;
        }

    }

    //开启WebSocket
    public function start()
    {
冯超鹏's avatar
冯超鹏 committed
58 59
        $url = config('public.swoolwebsocketurl');
        $por = config('public.swoolwebsocketurlpor');
冯超鹏's avatar
冯超鹏 committed
60
        $this->ws = new \swoole_websocket_server('0.0.0.0', 9502); //创建一个端口
冯超鹏's avatar
冯超鹏 committed
61 62 63 64 65 66 67 68
        $this->ws->set(array(
            'reactor_num' => 2, //reactor线程数
            'worker_num' => 4,    //worker进程数
            'backlog' => 128,   //Listen队列长度
            'max_request' => 10,//最大连接
            'daemonize'=>0,//守护进程
        ));
        $this->ws->on('open', function ($ws, $request) {
69 70 71 72 73 74 75 76
//            //链接成功
            $userdata =  new SwooleCommandController();
            $this->ws->push($request->fd,$userdata->userslist(),1);
            swoole_timer_tick(6000, function($timerId) use ($ws, $request) {
                $userdata =  new SwooleCommandController();
                $this->ws->push($request->fd,$userdata->userslist(),1);

            });
冯超鹏's avatar
冯超鹏 committed
77 78 79 80 81 82
        });

        //监听WebSocket消息事件
        $this->ws->on('message', function ($ws, $frame) {
        });

83
        $this->ws->on('request', function ($request, $response,$from_id,$fd) {
冯超鹏's avatar
冯超鹏 committed
84 85 86 87
        });
        //监听WebSocket连接关闭事件
        $this->ws->on('close', function ($ws, $fd) {
            echo "client:{$fd} is closed\n";
88
            swoole_timer_clear($this->time);
冯超鹏's avatar
冯超鹏 committed
89 90 91
        });

        $this->ws->start();
冯超鹏's avatar
冯超鹏 committed
92 93
    }
}