2016-07-07 12 views
0

WebSocketを使用するLaravel 5.2のアプリケーションを作成しています。私はHoaServerを使用してwebsocket接続、非常にうまく動作します。WebSocket with Laravel 5.2

悪いところは、このサーバーをコントローラとして作成する方法、または少なくとも私のモデルにアクセスできないことです。今は、DBクエリーを作成するために分割されたPDO接続を使用しています。

このサーバーをコントローラとして作成することは可能ですか、少なくとも、laravelモデルを通じてデータベースにアクセスできるかどうかは誰かが知っていますか?今

マイサーバー:

require_once(__DIR__.'/../vendor/autoload.php'); 

$PDO = new PDO('mysql:host=127.0.0.1:3306;dbname=DBNAME', "USER", "PASS"); 

$websocket = new Hoa\Websocket\Server(new Hoa\Socket\Server('ws://'.$ip.':'.$porta)); 

$websocket->on('open', function (Hoa\Event\Bucket $bucket) { 
    return; 
}); 

$websocket->on('message', function (Hoa\Event\Bucket $bucket) { 
    return; 
}); 

$websocket->on('close', function (Hoa\Event\Bucket $bucket) { 
    return; 
}); 

$websocket->run(); 

私が好きな私は方法がわからないこと、laravel eventを発射したことに最も近いです。 :/

//Socket server message event 
$server->on('message', function() { 
    //Fire your Laravel Event here 
}); 
+0

コンソールを使用しようとしましたか? https://laravel.com/docs/5.2/artisan – xmhafiz

+0

「make:controller」を意味する場合は、yep。私はLaravel、コントローラ、モデル、ビューを使ってシステムを作った。問題は、コントローラの中で新しいHoa \ Websocket \ Server()の動作方法を知りません^^ " –

+0

いいえ、make:consoleを使用してコンソールにコードを追加することを意味します。 – xmhafiz

答えて

3

私はあなたがすべきことは、コンソールコマンドを作成することだと思います。

php artisan make:console StartSocketServer --command=socket:start 

と最後にApp\Console\Kernelでコマンドを登録した後、あなたの端末からphp artisan socket:startを実行することができます

<?php 

namespace App\Console\Commands; 

use Illuminate\Console\Command; 

class StartSocketServer extends Command 
{ 
    /** 
    * The name and signature of the console command. 
    * 
    * @var string 
    */ 
    protected $signature = 'socket:start'; 

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

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

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle() 
    { 
     $websocket = new Hoa\Websocket\Server(new Hoa\Socket\Server('ws://'.$ip.':'.$porta)); 

     $websocket->on('open', function (Hoa\Event\Bucket $bucket) { 
      return; 
     }); 

     $websocket->on('message', function (Hoa\Event\Bucket $bucket) { 
      return; 
     }); 

     $websocket->on('close', function (Hoa\Event\Bucket $bucket) { 
      return; 
     }); 

     $websocket->run(); 
    } 
} 

を次のように、あなたは生成されたクラスを編集します。

私はHoaServerを一度も使用しませんでしたが、これはうまくいくと思います。

+0

ここのコンソールはhttps:// laravel .com/docs/5.2/artisan – sleimanx2

+0

完璧な男!ありがとう! –