java Pusher:如何获取Pusher中的私有频道订阅数?

jvlzgdj9  于 2023-04-04  发布在  Java
关注(0)|答案(3)|浏览(159)

**我想要什么:**我在推送器中订阅了一个私人频道后,如何获得订阅数。实际上我只想在2个用户之间进行私人聊天。
我所知道的和正在做的:.我知道我的私人通道可以由多个客户端(用户)订阅.所以每当服务器触发数据到私人通道然后所有订阅的用户得到该响应.
频道订阅java脚本代码(Client-1):

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="https://js.pusher.com/3.2/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;
    var pusher = new Pusher('PUSHER_KEY', {
      authEndpoint: 'http://localhost:8080/medecube/rest/initial/pusher/auth',
       auth: {
          headers: {
            'X-CSRF-Token': "12345"
          }
        } 

    });

    var channel = pusher.subscribe('private-channel');
    channel.bind('pusher:subscription_succeeded', function() {
        var triggered = channel.trigger('client-myEvent', { "message": "i am pusher client1" });
      }); 

  </script>
</head>

客户端2订阅的相同频道java脚本中的频道订阅代码(客户端2):

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="https://js.pusher.com/3.2/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;
    var pusher = new Pusher('PUSHER_KEY', {
      authEndpoint: 'http://localhost:8080/medecube/rest/initial/pusher/auth',
       auth: {
          headers: {
            'X-CSRF-Token': "12345"
          }
        } 

    });

    var channel = pusher.subscribe('private-channel');
    channel.bind('pusher:subscription_succeeded', function() {
        var triggered = channel.trigger('client-myEvent', { "message": "i am pusher client2" });
      }); 

  </script>
</head>

服务器触发事件:

推杆:Event recd : {"event":"client-myEvent","data":{"message":"i am pusher client2"},"channel":"private-channel"}
然后两个客户端得到相同的响应。我只想一个用户订阅只有1个频道。

xtupzzrd

xtupzzrd1#

您可以获取私人频道的订阅计数,而不必使用在线状态频道。但是,要获取私人频道的订阅计数,您必须通过推送器 Jmeter 板显式启用该功能,因为默认情况下未启用该功能:
https://pusher.com/docs/rest_api#method-get-channel

yr9zkbsy

yr9zkbsy2#

你需要使用 presence channels 来获取频道中成员的信息。在你的例子中,你可以用var count = presenceChannel.members.count;来获取订阅者的数量。

gmxoilav

gmxoilav3#

看看我的两个用户私聊的例子,我在其中验证一个用户当前是否在输入录制语音消息

channel.pusher.channel('private-chat.{{ $thread_id }}')
    .bind("pusher:subscription_count", (data) => {
        let activeUsers = data.subscription_count;
        console.log(activeUsers);
        if (activeUsers === 1) {
            @this.recordingVoice = false;
            @this.typing = false;
        }
    });

我使用Laravel Echo/Pusher和Livewire来解决这个问题。我将带有@this.recordingVoice = false的php变量发送到Livewire组件,当其他用户离开聊天室/dm收件箱时,该组件停止显示User X is currently recording a voice message...

相关问题