【问题标题】:Emit data to a specific user using Socket IO,Laravel,Redis,Angularjs使用 Socket IO、Laravel、Redis、Angularjs 向特定用户发送数据
【发布时间】:2015-07-11 16:19:19
【问题描述】:

我正在构建一个应用程序,让学生能够查看由管理员创建的日程安排。现在每个学生都有一个 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');
  });

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

  1. 有什么东西告诉我,我们可以用 redis 制作一个动态频道吗?可悲的是我没有任何经验。
  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);
    }
}

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

【问题讨论】:

  • 我也想知道解决方法...
  • 不知道你是怎么解决的?太糟糕了,文档没有透露太多关于它的信息......

标签: angularjs node.js laravel redis socket.io


【解决方案1】:

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

例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2013-01-03
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多