Laravel 5 使用laravel、Socket和Redis改变频道动态
!!! 服务器-套接字如何更改通道动态 例如,当我使用/fire/1进行广播时,我只想发送到/room/1 目前它发送到/room/1、/room/2、/room/3等,因为在这里默认情况下,服务器上的所有内容都订阅了“测试通道”。我就是搞不懂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
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函数中使用该变量的情况。