Laravel 和 Socket/Redis - 私人频道路线不工作

Laravel with Socket/Redis - Private channel routes not working

我有点卡在广播路线上了。我用 redis 设置了一个套接字服务器,并用 Laravel 配置了它。对于 public 频道,一切正常,但当涉及到私人或存在频道时,它以某种方式绕过了 laravel 广播路由。无法弄清楚如何以及为什么。

我附上了一个 repo link 所以你们也可以探索它。下面还有一些快速位。

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

routes/web-- 用于开火

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

谢谢。

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

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

但在 routes/channels.php:

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

听起来像是打字错误!