2015-05-01 11 views
5

Buduję aplikację, która daje uczniom możliwość zobaczenia ich harmonogramu, który jest tworzony przez administratora. Teraz każdy uczeń ma identyfikator group_id. Chcę aktualizować harmonogram w czasie rzeczywistym, więc zastosowałem ten samouczek: http://www.kodeinfo.com/post/realtime-app-using-laravel-nodejs-angularjs-redis. tutaj, co zrobiłem do tej pory.Emituj dane do określonego użytkownika przy użyciu Socket IO, Laravel, Redis, Angularjs

Event Handler:

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 (Event CRUD Method)

//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('/'); 
    } 
} 

więc w zasadzie jestem pchanie ostatni utworzony zdarzenie.

węzeł serwera:

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(); 
    }); 
}); 

kątowa Controller:

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

Zagadnienie to w jaki sposób można filtrować dane przesyłane do konkretnego ucznia.

  1. Coś mi mówi, że możemy stworzyć dynamiczny kanał z Redis? Niestety nie mam z tym żadnego doświadczenia.
  2. Próbowałem uruchomić wydarzenie po pobraniu danych według identyfikatora grupy studentów , co kończy się tym, że student widzi nowy harmonogram za każdym razem, gdy zmienia się identyfikator grupy.

//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); 
    } 
} 

Mam nadzieję, że było wystarczająco jasne, mam nadzieję dostać jakieś odpowiedzi dzięki.

+0

Chcę wiedzieć obejście na ten temat, jak również ... –

+0

Zastanawiam się, jak pan go rozwiązał? Szkoda, że ​​dokumenty nie zdradzają zbyt wiele ... – Shay

Odpowiedz

0

Musisz wysłać dane do każdego ucznia na oddzielnym kanale.

Przykład:

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