带有Laravel 5.2的WebSocket [英] WebSocket with Laravel 5.2

查看:227
本文介绍了带有Laravel 5.2的WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Websockets在Laravel 5.2中进行应用.对于Websocket连接,我使用的是 HoaServer ,它可以很好地工作.

I'm doing a aplication in Laravel 5.2 that uses websockets. For the websocket connection I'm using HoaServer, wich works very well.

糟糕的是,我不知道如何将该服务器用作控制器,或者至少对我的模型有需求,现在我正在使用分离的PDO连接来进行数据库查询.

The bad part is, I do not know how to make this server as a controller, or at least have acess to my models, right now I'm using a separeted PDO connection to make the DB querys.

有人知道是否有可能使该服务器作为控制器,或者至少可以通过laravel模型访问数据库?

Someone knows if it is possible to make this server as a controller or at least have acess to the database throught laravel models?

我的服务器现在:

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事件,我不知道该怎么做. :/

The closest that I fond was to fire an laravel event, that I do not know how. :/

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

推荐答案

我认为您应该做的是创建控制台命令.

I think that what you should do is to create a console command.

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

然后按如下所示编辑生成的类

and then you edit the generated class as follows

<?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();
    }
}

最后在App\Console\Kernel中注册命令后,您可以从终端运行php artisan socket:start.

Finally after registering the command in App\Console\Kernel you can run php artisan socket:start from your terminal.

我从未使用过HoaServer,但是我认为这应该可行.

I never used HoaServer , But i think this should work.

这篇关于带有Laravel 5.2的WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆