我正在构建一个应用程序,让学生能够查看由管理员创建的日程安排。现在每个学生都有一个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();
});
});
角度控制器:
studentSocket.on('schedule.update', function (data) {
$scope.events.length = 0;
$scope.populatePlan(JSON.parse(data));
inform.add('New Course Added');
});
问题是如何将发送的数据过滤到特定学生。
//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);
}
}
我希望我很清楚我真的希望得到一些答案谢谢。
答案 0 :(得分:0)
您需要在单独的频道上将数据发送给每个学生。
示例:
public function broadcastOn()
{
return ['Student.' . $this->student->Id];
}