問題描述
我需要在 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.
這篇關于laravel 5 中的簡單 websocket 實現的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!