laravel 5中的简单websocket实现

Has*_*han 19 php websocket cordova laravel-5 brain-socket

我需要在Laravel中实现非常简单和非常基本的websocket,以实现我的phonegap应用程序作为客户端和我的Laravel网站作为服务器之间的数据同步过程.我按照本教程http://www.binarytides.com/websockets-php-tutorial/来实现和测试websocket,它的工作原理.像这个我需要非常简单的laravel实现,我可以从js客户端调用我的控制器方法.客户端将是我的phonegap应用程序.我在laravel中找到了一些包含websocket的软件包,但是我发现很难实现它们.没有人与控制器交互,他们在这里和那里听事件和创建类,但在控制器中没有.我已经在Controller中编写了所有逻辑并使用ajax请求对其进行了测试,但现在我将通过websocket实现它,因为我需要双向通信来实现同步过程.我是Laravel的新手,所以请给我一些帮助.如果有人可以告诉我如何在laravel中集成about教程以便客户端可以直接调用控制器来发送数据,那将会非常棒.

Has*_*han 23

我最终使用了brainboxlabs的brainsocket(https://github.com/BrainBoxLabs/brain-socket).正如其文件所述的laravel 4包装,但它也适用于laravel 5而没有任何问题.

使用laravel 5安装此软件包.请按照上面github链接中的文档进行操作.它说在app文件夹中创建一个event.php文件和一些事件相关的代码.而不是这一步只需在app/Providers/EventServiceProvider.php文件中添加与事件相关的代码.在其引导方法中,添加该代码

Event::listen('generic.event',function($client_data){
    return BrainSocket::message('generic.event',array('message'=>'A message from a generic event fired in Laravel!'));
});

Event::listen('app.success',function($client_data){
    return BrainSocket::success(array('There was a Laravel App Success Event!'));
});

Event::listen('app.error',function($client_data){
    return BrainSocket::error(array('There was a Laravel App Error!'));
});
Run Code Online (Sandbox Code Playgroud)

在这一步之后,有一个添加步骤

require app_path().'/filters.php';
require app_path().'/events.php';
Run Code Online (Sandbox Code Playgroud)

在app/start/global.php中.您可以将此步骤留给laravel 5.

好的,所以Web套接字已经实现.您可以通过运行命令使用cmd启动websocket服务器进行测试artisan brainsocket:start.你可以选择提供端口工匠brainsocket:start 9000

另一个要求是调用控制器来执行其余任务.为此我直接编辑到提供程序包.我不推荐这个,因为它不是一个好方法.当您使用composer更新包时,您的更改将会丢失.所以你必须找到一个更好的选择.但它只是一线改变.

在vendor\brainboxlabs\brain-socket\src\BrainSocket\BrainSocketServer.php中,编辑了方法"start"并替换了代码

$this->server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new BrainSocketEventListener(
                        new BrainSocketResponse(new LaravelEventPublisher())
                    )
                )
            )
            , $port
        );
Run Code Online (Sandbox Code Playgroud)

$this->server = IoServer::factory(
            new HttpServer(
                new WsServer(
               new \FMIS\Http\Controllers\SynchronizationController(
                  new BrainSocketResponse(new LaravelEventPublisher())
                                            )
                )
            )
            , $port
        );
Run Code Online (Sandbox Code Playgroud)

在我的SynchronizationController文件中.

我在顶部添加了这个

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use BrainSocket\BrainSocketResponseInterface;
Run Code Online (Sandbox Code Playgroud)

实现这样的接口.

class SynchronizationController extends Controller implements MessageComponentInterface{
Run Code Online (Sandbox Code Playgroud)

并实现了这个接口的方法.

public function __construct(BrainSocketResponseInterface $response) {
        $this->clients = new \SplObjectStorage;
        $this->response = $response;
}

public function onOpen(ConnectionInterface $conn) {
        echo "Connection Established! \n";
}


public function onMessage(ConnectionInterface $conn, $msg){
 echo "this messge gets called whenever there is a messge sent from js client";
}

public function onClose(ConnectionInterface $conn) {
    echo "Connection {$conn->resourceId} has disconnected\n";
}

public function onError(ConnectionInterface $conn, \Exception $e) {
        $msg = "An error has occurred: {$e->getMessage()}\n";
        echo $msg;
        $conn->close();
}
Run Code Online (Sandbox Code Playgroud)

您必须更改这些方法才能实现您的功能.在此之后,您可以从您的js客户端拨打电话.而且您也不需要使用其js库.您只需使用本教程http://www.binarytides.com/websockets-php-tutorial/中描述的js客户端发送数据.

如果有人需要更多关于其实施的帮助,请告诉我.