laravel使用Workerman搭建簡易聊天室

前言


laravel5.4實現(xiàn) -> http://www.itdecent.cn/p/d808dfa8b2d7
以下正文


實現(xiàn)一個簡易的聊天室,
到這里為止通過對照那篇文章感覺swoole跟workerman實現(xiàn)還是挺像的.

參考了文檔還有一篇文章↓

http://www.workerman.net/doc
 http://www.itdecent.cn/p/4ad04f8ff907

app/Console/Commands/Workerman.php

<?php

namespace App\Console\Commands;

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

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

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

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

    public function fire()
    {
        $arg = $this->argument('action');
        switch ($arg) {
            case 'start':
                $this->info('workerman observer started');
                $this->start();
                break;
            case 'stop':
                $this->info('stoped');
                break;
            case 'restart':
                $this->info('restarted');
                break;
        }
    }

    private function start()
    {
        global $text_worker;
        // 創(chuàng)建一個Worker監(jiān)聽9130端口,使用http協(xié)議通訊
        $text_worker = new Worker("websocket://0.0.0.0:9130");
/*        $http_worker->transport = 'http';*/
        // 啟動4個進程對外提供服務(wù)
        $text_worker->count = 4;
        $handler = \App::make('handler\WorkermanHandler');
        $text_worker->onConnect = array($handler,"handle_connection");
        $text_worker->onMessage = array($handler,"handle_message");
        $text_worker->onClose = array($handler,"handle_close");

        // 運行worker
        Worker::runAll();
    }

    protected function getArguments()
    {
        return array(
            array('action',InputArgument::REQUIRED,'start|stop|restart'),
            );
    }

}

app/handle/WorkermanHandler.php

<?php
namespace handler;

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

class WorkermanHandler
{

    protected $global_uid = 0;

    //當(dāng)客戶端連上來時分配uid,并保存鏈接,并通知所有客戶端
    public function handle_connection($connection){
      global $text_worker, $global_uid;
      //為這個鏈接分配一個uid
      $connection->uid = ++$global_uid;
      foreach($text_worker->connections as $conn){
        $conn->send("user:[{$connection->uid}] online");
      }
    }

    //當(dāng)客戶端發(fā)送消息過來時,轉(zhuǎn)發(fā)給所有人
    public function handle_message($connection,$data){
      global $text_worker;
      foreach($text_worker->connections as $conn){
        $conn->send("user:[{$connection->uid}] said:$data");
      }
    }

    //當(dāng)客戶端斷開時,廣播給所有客戶端
    public function handle_close($connection){
      global $text_worker;
      foreach($text_worker->connections as $conn){
        $conn->send("user:[{$connection->uid}] logout");
      }
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容