vluzrmos / lumen-socketio
此包已被弃用且不再维护。作者建议使用 http://lumen.laravel.com/docs/events#broadcasting-events 包。
Laravel Lumen Socketio 广播。
v0.1.5
2016-04-21 15:02 UTC
Requires
- illuminate/redis: ~5.0
- illuminate/support: ~5.0
- predis/predis: 1.0.*
README
该包已被弃用,请考虑使用 LARAVEL|LUMEN 内置解决方案:https://laravel.net.cn/docs/5.2/events#broadcast-data
安装
composer require vluzrmos/lumen-socketio
在 bootstrap/app.php
中添加服务提供者
$app->register('Vluzrmos\Socketio\SocketioServiceProvider');
配置
安装 NodeJs 依赖
npm install --save express http-server redis ioredis socket.io
将文件 vendor/vluzrmos/lumen-socketio/Vluzrmos/Socketio/socket.js
复制到您的项目根目录。
修改您想要的任何内容,查看代码:socket.js
注意:请记得安装一个 Redis 服务器
在您的视图中,您必须使用 socket.io.js
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> Lumen Socket.IO </title> <script src="//cdn.socket.io/socket.io-1.3.5.js"></script> </head> <body> <script type="text/javascript"> var socket = io('http://localhost:8080'); //Some host and port configured in socket.js socket.on('channel:awesome-event', function (data) { console.log(data); }); </script> </body> </html>
使用方法
运行您的 socket.io 服务器
# that socket.js file is in your project root
node socket.js
在您的 Lumen 应用中
$app->get('/publish', function() { publish('channel', 'awesome-event', 'An message'); publish('channel', 'awesome-event', ['message' => 'An message', 'user' => \App\User::first()]); }); //or, in your controller or some else method (using Dependency Injection) public function publishSomethingAwesome(\Vluzrmos\Socketio\Contracts\Broadcast $broadcast){ $broadcast->publish('channel', 'awesome-event', 'An message'); // or just use the helper without inject \Vluzrmos\Socketio\Contracts\Broadcast publish('channel', 'awesome-event', 'An message'); }
完成,运行您的 lumen 应用
php artisan serve