laravel 5 中的简单 websocket 实现
我需要在 Laravel 中实现非常简单和非常基本的 websocket,以在作为客户端的 phonegap 应用程序和作为服务器的 Laravel 网站之间实现数据同步过程.我跟着这个教程http://www.binarytides.com/websockets-php-tutorial/ 来实现和测试 websocket,它可以工作.像这个我需要非常简单的 Laravel 实现,我可以在其中从 js 客户端调用我的控制器方法.客户端将是我的 phonegap 应用程序.我在 laravel 中找到了一些带有教程的 websocket 包,但我发现很难实现它们.没有人与控制器进行交互,他们在各处监听事件并创建类,但不在控制器中.我已经在 Controller 中编写了所有逻辑并使用 ajax 请求对其进行了测试,但现在我将通过 websocket 实现它,因为我需要双向通信来实现同步过程.我是 Laravel 的新手,所以请给我一些帮助.如果有人能告诉我如何在laravel中集成about教程,以便客户端可以直接调用控制器发送数据,那就太好了.
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.
推荐答案
我最终使用了brainboxlabs的brainsocket (https://github.com/BrainBoxLabs/brain-socket) .正如它的文档所说,它的 laravel 4 包但它也可以与 laravel 5 一起使用,没有任何问题.
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.
要使用 laravel 5 安装此包.请按照上述 github 链接上的文档进行操作.它说在应用程序文件夹中创建一个 event.php 文件和一些与事件相关的代码.而不是这一步,只需在 app/Providers/EventServiceProvider.php 文件中添加与事件相关的代码.在其引导方法中,添加该代码
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';
在 app/start/global.php 中.你可以把这一步留给 Laravel 5.
in app/start/global.php . You can leave this step for laravel 5.
好的,Web 套接字已经实现.您可以通过运行命令 artisan brainsocket:start
使用 cmd 启动 websocket 服务器进行测试.你可以选择为它提供 port artisan Brainsocket:start 9000
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
另一个要求是调用控制器来执行其余的任务.为此,我直接编辑到提供程序包中.我不推荐这样做,因为这不是一个好方法.当您使用 Composer 更新您的包时,您的更改将丢失.所以你必须找到更好的选择.但它只是一行更改.
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.
在 vendorrainboxlabsrain-socketsrcBrainSocketBrainSocketServer.php 中,我编辑了方法start"中的代码并替换
In vendorrainboxlabsrain-socketsrcBrainSocketBrainSocketServer.php i edited code in method "start" and replace
$this->server = IoServer::factory(
new HttpServer(
new WsServer(
new BrainSocketEventListener(
new BrainSocketResponse(new LaravelEventPublisher())
)
)
)
, $port
);
与
$this->server = IoServer::factory(
new HttpServer(
new WsServer(
new FMISHttpControllersSynchronizationController(
new BrainSocketResponse(new LaravelEventPublisher())
)
)
)
, $port
);
在我的 SynchronizationController 文件中.
And in my SynchronizationController file.
我在上面添加了这个
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
use BrainSocketBrainSocketResponseInterface;
实现的界面是这样的.
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!
";
}
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
";
}
public function onError(ConnectionInterface $conn, Exception $e) {
$msg = "An error has occurred: {$e->getMessage()}
";
echo $msg;
$conn->close();
}
您必须更改这些方法才能实现您的功能.在此之后,您可以从您的 js 客户端调用.你也不需要使用它的 js 库.您只需使用本教程中描述的 js 客户端发送数据 http://www.binarytides.com/websockets-php-tutorial/ .
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.
相关文章