【问题标题】:Laravel with Socket/Redis - Private channel routes not working带有 Socket/Redis 的 Laravel - 私有通道路由不起作用
【发布时间】:2018-10-27 09:58:59
【问题描述】:

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

我附上了一个回购链接,所以你们也可以探索它。另外,下面还有一些快速的位。

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

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

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

    但在routes/channels.php:

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

    听起来像是错字!

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 2017-10-31
      • 2017-10-09
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2016-04-21
      • 2023-04-06
      相关资源
      最近更新 更多