在laravel中简单的websocket实现5 [英] Simple websocket implementation in laravel 5

查看:1799
本文介绍了在laravel中简单的websocket实现5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现非常简单和非常基本的websocket在Laravel实现我的phonegap应用程序之间的数据同步过程作为客户端和我的Laravel网站作为服务器。我按照本教程 http://www.binarytides.com/websockets-php-tutorial/实现和测试websocket和它工作。像这个我需要非常简单的laravel实现,我可以从js客户端调用我的控制器方法。客户端将是我的phonegap应用程序。我发现一些websocket的包在laravel与教程,但我发现很难实现它们。没有人与控制器交互,他们正在侦听事件并在这里​​和那里创建类,但不在控制器中。我已经写了我所有的逻辑在Controller和测试它与ajax请求,但现在我将实现它由websocket,因为我需要双向通信来实现同步过程。我是新来的Laravel所以请提供我一些帮助。如果有人可以告诉我如何在laravel中整合关于教程,以便客户端可以直接调用控制器发送数据,这将是如此之大。

解决方案

我最后使用brainboxlabs的brainsocket( https://github.com/BrainBoxLabs/brain-套接字)。正如它的文件说它的laravel 4包,但它也适用于laravel 5没有任何问题。



使用laravel安装此软件包5.按照上述github链接上的文档。它在哪里说在应用程序文件夹中创建一个event.php文件和一些事件相关的代码。而不是这一步简单地添加该事件相关的代码在app / Providers / EventServiceProvider.php文件。在其启动方法中,添加

  Event :: listen('generic.event',function($ client_data) {
return BrainSocket :: message('generic.event',array('message'=>'一个来自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('Laravel App Error!'));
});

此步骤之后,添加

  require app_path()。'/ filters.php'; 
require app_path()。'/ events.php'; app / start / global.php中的

您可以为laravel 5离开此步骤。



好,所以Web套接字已实现。您可以通过运行命令 artisan brainsocket:start ,使用cmd启动websocket服务器进行测试。您可以选择为它提供port artisan brainsocket:start 9000



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



在vendor \brainboxlabs\brain-socket\src\BrainSocket\BrainSocketServer.php我编辑的代码在方法开始并替换

  $ this-> server = IoServer :: factory(
new HttpServer b new WsServer(
new BrainSocketEventListener(
new BrainSocketResponse(new LaravelEventPublisher())



,$ port
) ;

  $ this-> server = IoServer :: factory(
new HttpServer(
new WsServer(
new \FMIS\Http\Controllers\SynchronizationController(
new BrainSocketResponse(new LaravelEventPublisher())



,$ port
);



在我的SynchronizationController文件中。



我将此添加到顶部

 使用Ratchet\ MessageComponentInterface; 
使用Ratchet\ConnectionInterface;
使用BrainSocket\BrainSocketResponseInterface;

这样实现的界面。

  class SynchronizationController extends Controller implements MessageComponentInterface {

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

public function onOpen(ConnectionInterface $ conn){
echoConnection Established!\\\
;
}


public function onMessage(ConnectionInterface $ conn,$ msg){
echo每当有来自js客户端发送的消息时,这个消息被调用;
}

public function onClose(ConnectionInterface $ conn){
echoConnection {$ conn-> resourceId}已断开连接\\\
;
}

public function onError(ConnectionInterface $ conn,\Exception $ e){
$ msg =发生错误:{$ e-> getMessage } \\\
;
echo $ msg;
$ conn-> close();
}

您必须更改这些方法才能实现您的功能。之后,您可以从您的js客户端调用。你也不需要使用它的js库。您只需使用本教程中的js client describe发送数据 http://www.binarytides.com/websockets -php-tutorial /



如果有人需要更多帮助,请告诉我们。


I need to implement very simple and very basic websocket in Laravel to implement data synchronization process between my phonegap app as client and my Laravel Website as server. I followed this tutorial http://www.binarytides.com/websockets-php-tutorial/ to implement and test websocket and it works. Like this one i need very simple laravel implementation where i can call my controller method from js client. Client will be my phonegap application. I found some packages for websocket in laravel with tutorials, but i found difficult to implement them. No one was interacting with controllers, they were listening to events and creating classes here and there but not in controllers. I have written all my logic in Controller and tested it with ajax request but now i will implement it by websocket because i need bidirectional communication to implement Synchronization process. I am new to Laravel so please provide me some help. Also it will be so great if somebody can tell me how to integrate the about tutorial in laravel to so that client can directly call controller to send data.

解决方案

I ended up using brainboxlabs's brainsocket (https://github.com/BrainBoxLabs/brain-socket) . As its document says its laravel 4 package but it also works with laravel 5 without any issue.

To install this package with laravel 5. Follow the documentation on the above github link. Where it says to create an event.php file in app folder and some events related code. Instead of this step simply add that event related code in app/Providers/EventServiceProvider.php file. In its boot method, add that code which is

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!'));
});

After this step there was a step of adding

require app_path().'/filters.php';
require app_path().'/events.php';

in app/start/global.php . You can leave this step for laravel 5.

Ok so Web socket has been implemented. You can test by starting the websocket server using cmd by running the command artisan brainsocket:start. You can optionally provide it the port artisan brainsocket:start 9000

Another requirement was to call controller to to perform rest of the task. For this i directly edited into to provider package. I do not recommend this as it is not a good way. When you will update your package using composer your changes will be lost. So you have to find a better option. But its just a one line change.

In vendor\brainboxlabs\brain-socket\src\BrainSocket\BrainSocketServer.php i edited code in method "start" and replace

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

with

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

And in my SynchronizationController file.

I added this on top

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use BrainSocket\BrainSocketResponseInterface;

Implemented interface like this.

class SynchronizationController extends Controller implements MessageComponentInterface{

and implemented the methods of this interface.

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

You have to change in these methods to implement your functionality. After this you can call from your js client. And you are not required to use its js library as well. You simply send data using js client describe in this tutorial http://www.binarytides.com/websockets-php-tutorial/ .

Let me know if somebody need any more help regarding its implementation.

这篇关于在laravel中简单的websocket实现5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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