laravel 事件廣播


Laravel 5.1 之中新加入了事件廣播的功能,作用是把服務器中觸發的事件通過websocket服務通知客戶端,也就是瀏覽器,客戶端js根據接受到的事件,做出相應動作。本文會用簡單的代碼展示一個事件廣播的過程。 依賴: redis nodejs, socket.io laravel 5.1

配置: config/broadcasting.php中,如下配置'default' => env('BROADCAST_DRIVER', 'redis'),,使用redis作為php和js的通信方式。 config/database.php中配置redis的連接。 定義一個被廣播的事件: 根據Laravel文檔的說明,想讓事件被廣播,必須讓Event類實現一個Illuminate\Contracts\Broadcasting\ShouldBroadcast接口,並且實現一個方法broadcastOn。broadcastOn返回一個數組,包含了事件發送到的channel(頻道)。如下:
namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class SomeEvent extends Event implements ShouldBroadcast
{
  use SerializesModels;

  public $user_id;

  /**
   * Create a new event instance.
   *
   * @return void
   */
  public function __construct($user_id)
  {
    $this->user_id = $user_id;
  }

  /**
   * Get the channels the event should be broadcast on.
   *
   * @return array
   */
  public function broadcastOn()
  {
    return ['test-channel'];
  }
}
 
        
被廣播的數據: 默認情況下,Event中的所有public屬性都會被序列化后廣播。上面的例子中就是$user_id這個屬性。你也可以使用broadcastWith這個方法,明確的指出要廣播什么數據。例如:
public function broadcastWith()
{
  return ['user_id' => $this->user_id];
}
 
        
Redis和Websocket服務器: 需要啟動一個Redis,事件廣播主要依賴的就是redis的sub/pub功能,具體可以看redis文檔 需要啟動一個websocket服務器來和client通信,建議使用socket.io,代碼如下:
var app = require('http').createServer(handler);
var io = require('socket.io')(app);

var Redis = require('ioredis');
var redis = new Redis('6379', '192.168.1.106');

app.listen(6001, function() {
  console.log('Server is running!');
});

function handler(req, res) {
  res.writeHead(200);
  res.end('');
}

io.on('connection', function(socket) {
  console.log('connected');
});

redis.psubscribe('*', function(err, count) {
  console.log(count);
});

redis.on('pmessage', function(subscribed, channel, message) {
  console.log(subscribed);
  console.log(channel);
  console.log(message);

  message = JSON.parse(message);
  io.emit(channel + ':' + message.event, message.data);
});
這里需要注意的是redis.on方法的定義,接收到消息后,給client發送一個事件,事件名稱為channel + ':' + message.event。 客戶端代碼: 客戶端我們也使用socket.io,作為測試,代碼盡量簡化,僅僅打印一個接受到的數據即可。如下:
var socket = io('http://localhost:6001');
socket.on('connection', function (data) {
  console.log(data);
});
socket.on('test-channel:App\\Events\\SomeEvent', function(message){
  console.log(message);
});
console.log(socket);
服務器觸發事件: 直接在router中定義個事件觸發即可。如下:
Route::get('/event', function(){
  Event::fire(new \App\Events\SomeEvent(3));
  return "hello world";
});
測試: 啟動redis 啟動websocket 打開帶有客戶端代碼的頁面,可以看到websocket已經連接成功。 觸發事件,打開另一個頁面 localhost/event。 這時就可以發現,第一個頁面的console中打印出了Object{user_id: 3},說明廣播成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM