Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5 使用laravel、Socket和Redis改变频道动态_Laravel 5_Redis_Socket.io_Vuejs2 - Fatal编程技术网

Laravel 5 使用laravel、Socket和Redis改变频道动态

Laravel 5 使用laravel、Socket和Redis改变频道动态,laravel-5,redis,socket.io,vuejs2,Laravel 5,Redis,Socket.io,Vuejs2,!!! 服务器-套接字如何更改通道动态 例如,当我使用/fire/1进行广播时,我只想发送到/room/1 目前它发送到/room/1、/room/2、/room/3等,因为在这里默认情况下,服务器上的所有内容都订阅了“测试通道”。我就是搞不懂 var server = require('http').Server(); var io = require('socket.io')(server); var Redis = require('ioredis'); var redis = new

!!! 服务器-套接字如何更改通道动态

例如,当我使用/fire/1进行广播时,我只想发送到/room/1

目前它发送到/room/1、/room/2、/room/3等,因为在这里默认情况下,服务器上的所有内容都订阅了“测试通道”。我就是搞不懂

var server = require('http').Server();

var io = require('socket.io')(server);

var Redis = require('ioredis');
var redis = new Redis();


io.on('connection', function(socket){
    console.log('New User Conected here');

 redis.subscribe('test-channel');

redis.on('message', function(subscribed ,channel, message) {
    console.log(channel);
    message = JSON.parse(message);

    socket.emit(channel + ':' + message.event, message.data);
});

socket.on('joinRoom', function(room ){

    console.log('Join in  this Room '+ room);
    socket.join(room);

    });

});
server.listen(3000);

    event | php


    public function broadcastOn()
    {

         return ['test-channel']; // static

    }

向广播事件添加属性,然后将其传递给构造函数

class Message implements ShouldBroadcast{
    use SerializesModels;

    protected $channel;

    public function __construct($channel){
        $this->channel = $channel;
    }

    public function broadcastOn(){
       return [$this->channel];
    }
}

然后在触发事件时,传入通道:
event(新消息($channel))

谢谢乔,我正在尝试,但如何在服务器套接字中检索它
redis.subscribe('channel');subscribe('*',函数(err,count){})你能帮我吗?你还没有提供足够的上下文来回答如何订阅前端动态定义的频道,但肯定这只是将频道名称作为变量传递给视图,然后在subscribe函数中使用该变量的情况。