美文网首页PHP编程之路
laravel5.4+workerman 简单版

laravel5.4+workerman 简单版

作者: Nannn_楠 | 来源:发表于2017-11-28 14:14 被阅读3524次

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%以上的问题.

相关文章

网友评论

    本文标题:laravel5.4+workerman 简单版

    本文链接:https://www.haomeiwen.com/subject/jfosbxtx.html