Looking to hire Laravel developers? Try LaraJobs

laravel-workerman maintained by aoeng

Description
laravel workerman websocket
Author
Last update
2022/08/06 05:35 (1.0.2)
License
Links
Downloads
145

Comments
comments powered by Disqus

Workerman 的Laravel 扩展

安装

composer require aoeng/laravel-workerman

php artisan vendor:publish --tag=workerman

使用

  • 本扩展是以事件的形式处理Workerman的各种消息 具体可查看 laravel事件系统

  • 配置文件:config/workerman.php

  • 可用事件

Aoeng\Laravel\Workerman\Events\WorkermanOnWorkerStart::class
Aoeng\Laravel\Workerman\Events\WorkermanOnConnect::class
Aoeng\Laravel\Workerman\Events\WorkermanOnWebSocketConnect::class
Aoeng\Laravel\Workerman\Events\WorkermanOnMessage::class
Aoeng\Laravel\Workerman\Events\WorkermanOnClose::class
Aoeng\Laravel\Workerman\Events\WorkermanOnWorkerStop::class
  • 监听器参考
namespace App\Listeners;

use Aoeng\Laravel\Workerman\Events\WorkermanOnMessage;
use Aoeng\Laravel\Workerman\Facades\Gateway;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class HandleSocketMessage
{
    /**
     * Handle the event.
     *
     * @param WorkermanOnMessage $event
     * @return void
     */
    public function handle(WorkermanOnMessage $event)
    {
        info('测试', [$event->message]);
        Gateway::sendToCurrentClient('你说的对');
    }
}


  • 注意 laravel事件默认禁用自动发现可通过以下方式开启或者进行手动绑定
// Providers/EventServiceProvider.php
public function shouldDiscoverEvents()
{
    return true;
} 

  • 注意每次修改完成后请重启workerman服务

  • 启动服务

php artisan workerman start|stop|restart|reload|status [--d]
//例如 
 Aoeng\Laravel\Workerman\Facades\Gateway::isUidOnline($uid);
  • 前端链接请每30s发送一个心跳包