laravel5.4+workerman 簡單版

emmmm...之前那個是5.1的5.4好像有點不一樣
首先composer workerman

composer require workerman/workerman

1.服務端

php artisan make:command workerman

然后>/app/Console/Commands下面就會創建一個文件workerman.php

我們改一改signature description 還有handle方法...↓
記得引入workerman

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App;
use Workerman\Worker;

class WorkermanServer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workerman:command {action} {-d}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Workerman Server';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $ws = new Worker("websocket://0.0.0.0:9011");

        $ws->count = 4;

        $ws->onConnect = function($connection)
        {
            echo "new connection\n";
        };

        $ws->onMessage = function($connection, $data)
        {
            echo $data."\n";
            
            $connection->send('hello1');
        };

        $ws->onClose = function($connection)
        {
            echo "Connection closed\n";
        };

        // Run worker
        Worker::runAll();
    }
}

然后...


jianshu1.png

就Ok了...

2.測試一下

打開瀏覽器輸入ip:端口


jianshu4.png

這個不管他↓

jianshu3.png

呼出控制臺...


jianshu2.png

就Ok啦


出問題了復制報錯信息百度,能解決90%以上的問題.

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容