带有Socket / Redis的Laravel-专用频道路由不起作用

时间:2018-10-27 09:58:59

标签: laravel sockets node-redis web-notifications laravel-broadcast

我有点被广播频道困住了。我用redis设置了一个套接字服务器,并用Laravel配置了它。对于公共频道,一切正常,但是对于私人频道或在线状态频道,它以某种方式绕过了laravel广播路径。无法弄清楚如何以及为什么。

我已经附加了一个repo链接,所以你们也可以探索它。加上一些快捷方式,也在下面。

https://github.com/bilahdsid/socket-laravel/tree/socket

TestEvent.php

class TestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $data;

    public function __construct()
    {
        $this->data = array(
            'power'=> '10'
        );
    }

    public function broadcastOn()
    {
        return new PrivateChannel('test-channel1');
    }

    public function broadcastWith()
    {
        return $this->data;
    }
}

server.js

    var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();


redis.subscribe('private-test-channel1', function(err, count) {

  console.log(err);
});

redis.on('connection',function (socket,channel) {

  console.log(socket+''|+channel);
});

redis.on('message', function(channel, message) {
  console.log('Message Recieved: ' + message);
  message = JSON.parse(message);
  io.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
  console.log('Listening on Port 3000');
});

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

路线/网络-触发

Route::get('/', function () {
    return view('home');
});

Route::get('fire', function () {
    // this fires the event
    broadcast(new App\Events\TestEvent());
    return "event fired";
});

routes / channel.php-以下行不起作用-主要问题

Broadcast::channel('private-test-channel', function ($user, $id) {

    echo '1111'; exit;
    return (int) $user->id === (int) $id;
});

谢谢。

1 个答案:

答案 0 :(得分:0)

据我所知,您正在定义一个名称为test-channel1的频道:

public function broadcastOn()
{
    return new PrivateChannel('test-channel1');
}

但在routes/channels.php中:

Broadcast::channel('private-test-channel', function ($user, $id) {

听起来像错字!