laravel整合workerman 比特神话 不是很好的办法 参考

本文介绍整合workerman来利用worker的websocker server功能

引入workerman

下载workerman然后放在laravel项目根目录

新建一个Artisan命令


namespace App\Console\Commands;

use App\Http\Controllers\BehaviorController;
use App\Models\Behavior;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Workerman\Worker;

require_once __DIR__ . '/../../../Workerman/Autoloader.php';

class Ws extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ws';

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

    protected $behaviorController;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->behaviorController = app(BehaviorController::class);
        parent::__construct();
    }

    /** 
     * Execute the console command. 
     * 
     * @return mixed 
     */ 
    public function handle() 
    { 
        global $argv; 
//        $argv[1] = $this->ask('Workman command:{start|stop|restart|reload|status|connections} ', 'start'); 
        $argv[1] =  'start'; 
        $this->info($argv[0]); 
        // 创建一个Worker监听2346端口,使用websocket协议通讯 
        $ws_worker = new Worker("websocket://0.0.0.0:2346"); 
        // 启动4个进程对外提供服务 
        $ws_worker->count = 4; 
        // 当收到客户端发来的数据后返回hello $data给客户端 
        $ws_worker->onMessage = function ($connection, $data) { 
            // 向客户端发送hello $data 
            $connection->send('hello ' . $data); 
            Log::debug($connection->id . " send data\n" . $data); 
            $item = new Behavior(); 
            $item->type = 'pv'; 
            $data = json_decode($data); 
            $connection->data = $data; 
            $this->behaviorController->storePV($data, $item); 
            $user = User::find($data->user); 
            if ($user) $user->behaviors()->save($item); 
            else $connection->data = null; 
            Log::debug($item->id); 
        }; 
        $ws_worker->onClose = function ($connection) { 
            if ($connection->data) 
                $this->behaviorController->storePV($connection->data); 
            Log::debug($connection->id . ' close'); 
            Log::debug(json_encode($connection->data)); 
        }; 
        // 运行worker 
        Worker::runAll(); 
    } 
} 

运行workerman

php artisan ws

视图中使用WebSocket连接服务器


 ws = new WebSocket('ws://{{$_SERVER["SERVER_ADDR"]}}:2346');
 wsdate = new Date();
 ws.onopen = function () {
 ws.send(JSON.stringify({
            'user': '{{auth()->check()?auth()->user()->id:0}}',
            'url': '{{$_SERVER['REQUEST_URI']}}',
            'time': Math.round(wsdate.getTime() / 1000)
   }));
 };
 ws.onmessage = function (e) {
        console.log('收到服务端的消息:' + e.data);
 };
 ws.onclose = function (e) {
};



Original url: Access
Created at: 2018-12-20 10:27:01
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论