0

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

我附上了一个回购链接,所以你们也可以探索它。加上一些快速位也在下面。

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

测试事件.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;
    }
}

服务器.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;
});

谢谢。

4

1 回答 1

0

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

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

但在routes/channels.php

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

听起来像是错字!

于 2019-09-02T05:57:57.793 回答