【问题标题】:Socket.io and Redis broadcast to all clients except senderSocket.io 和 Redis 向除发送方之外的所有客户端广播
【发布时间】:2017-02-26 10:31:13
【问题描述】:

我在后端使用 Laravel 来制作聊天应用程序。但是当我使用redis 时,我不能使用broadcast.send 功能。现在如何将消息发送给除发件人以外的所有客户端?此代码有错误:

io.broadcast.send(channel + ':' + message.event, message.data);

这都是我的服务器代码:

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('chat');

redis.on('message', function(channel, message) {
    console.log('Message Recieved: ' + message);
    message = JSON.parse(message);

    // send all client except sender
    io.broadcast.send(channel + ':' + message.event, message.data); 

});


io.sockets.on('connection', function(socket) {      
    console.log('User Connect with ID: ' + socket.id);         
});

// run server
http.listen(3001, function(){
    console.log('Listening on Port 3001');
});

和客户端代码:

var socketClient = io(':3001');
socketClient.on("chat:App\\Events\\ChatEvent", function(message){
        // increase the power everytime we load test route
        console.log(message);       
    });

ChatEvent.php

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ChatEvent extends Event implements ShouldBroadcast
{
    use SerializesModels;

    public $message;

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


    public function broadcastOn()
    {
        return ['chat'];
    }
}

【问题讨论】:

    标签: node.js laravel redis socket.io broadcast


    【解决方案1】:

    尝试使用通配符

    class messageEvent extends Event implements ShouldBroadcast
    {
        use SerializesModels;
    
        public $userid;
    
        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct($user)
        {
    
            if($user==\Auth::guard('admin')->user()->id){
                      return false;
             }
            $this->userid = $user;
    
    
        }
    
        /**
         * Get the channels the event should be broadcast on.
         *
         * @return array
         */
        public function broadcastOn()
        {   
            return ['ChatEvent-'.$this->userid];
        }
    }
    

    在 Js 中进行更改

        var socketClient = io(':3001');
        socketClient.on("chat:App\\Events\\ChatEvent-<?php echo \Auth::guard('admin')->user()->id; ?>", function(message){
    
                console.log(message);       
            });
    

    【讨论】:

    • 谢谢@ronak,但我想你不明白我的意思。我想向所有客户端广播消息,除了带有 socket.io 服务器配置的发送者。
    • @Ehsan "if($user==\Auth::guard('admin')->user()->id)" 在这种情况下,事件将由 sender 和 sender 触发在这种情况下将被忽略..
    【解决方案2】:

    您可以使用附加参数扩展 emit() 函数,例如 send_to_self=True,它与 broadcast=True 或房间值将配置是否应包含当前客户端。默认情况下,事情将保持不变,但如果 send_to_self 设置为 False,则遍历所有客户端以通知的循环将跳过当前连接。要检测哪一个是当前的,您可以使用 request.namespace,它是活动客户端的命名空间对象。

    所以你的代码是

    io.emit(channel + ':' + message.event, message.data,send_to_self=false);
    

    【讨论】:

      【解决方案3】:

      您可以使用辅助广播来执行此操作,然后使用方法toOthers();
      更多详情请查看文档https://laravel.com/docs/5.8/broadcasting#only-to-others

      【讨论】:

        猜你喜欢
        • 2023-02-01
        • 1970-01-01
        • 2014-07-29
        • 2015-05-29
        • 1970-01-01
        • 2019-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-21
        相关资源
        最近更新 更多