使用Socket IO,Laravel,Redis,Angularjs向特定用户发送数据

我正在构build一个应用程序,让学生能够看到由pipe理员创build的时间表。 现在每个学生都有一个group_id。 我想实时更新时间表,所以我应用了这个教程http://www.kodeinfo.com/post/realtime-app-using-laravel-nodejs-angularjs-redis 。 这里我到目前为止所做的。

事件处理程序

namespace echooly\Handlers; use Redis; use Response; class StudentScheduleUpdatedEventHandler { CONST EVENT = 'schedule.update'; CONST CHANNEL = 'schedule.update'; public function handle($data) { $redis = Redis::connection(); $redis->publish(self::CHANNEL, $data); } } 

AdministrationController(事件CRUD方法)

 //Create an event public function createEvent() { if(Auth::Admin()->check()) { $eventDetail = Input::all(); $event = Planing::create($eventDetail); $event->save(); Event::fire(\echooly\Handlers\StudentScheduleUpdatedEventHandler::EVENT, array($event)); } else { return Redirect::intended('/'); } } 

所以基本上我推动了最新的活动

节点服务器

 var express = require('express'), http = require('http'), server = http.createServer(app); var app = express(); const redis = require('redis'); const io = require('socket.io'); const client = redis.createClient(); server.listen(3000, 'localhost'); console.log("Listening....."); io.listen(server).on('connection', function(client) { const redisClient = redis.createClient(); redisClient.subscribe('schedule.update'); console.log("Redis server running....."); redisClient.on("message", function(channel, message) { client.emit(channel, message); }); client.on("getGroup", function(groupId) { console.log(groupId); }); client.on('disconnect', function() { redisClient.quit(); }); }); 

angular度控制器

  studentSocket.on('schedule.update', function (data) { $scope.events.length = 0; $scope.populatePlan(JSON.parse(data)); inform.add('New Course Added'); }); 

问题是如何将发送的数据过滤到特定的学生。

  1. 有什么事情告诉我,我们可以用redis制作一个dynamic频道? 可悲的是我没有任何经验。
  2. 我试图通过学生组ID访问数据时触发事件,最终导致学生在组ID改变时看到新的时间表。

 //Show Planing by group public function showPlaningsByGroup($id){ if(Auth::Admin()->check()){ $planing = Planing::with('Course')->where('group_id',$id)->get(); Event::fire(\echooly\Handlers\StudentScheduleUpdatedEventHandler::EVENT, array($planing)); return Response::json($planing); } } 

我希望我很清楚,我真的希望得到一些答案,谢谢。

您需要通过单独的渠道将数据发送给每个学生。

例:

 public function broadcastOn() { return ['Student.' . $this->student->Id]; }