dongyuan7981 2019-05-14 19:01
浏览 162

在pusher.js php中发送者和接收者聊天中的相同消息

I am new to PusherJS and developing a real-time chat app using PHP. Everything working fine but when I press enter to send message, it append it in sender's box and into receiver's box also. How I can differentiate it on the basis of session or user_id. See there are same message is sender and receiver's box: enter image description here

Real-Time.php:

$options = array(
    'cluster' => 'ap2',
    'useTLS' => true
);
$pusher = new Pusher\Pusher(
    'c575a7********edb87d',
    '8fee27********57fdd2',
    '7***6*',
    $options
);
$pusher->trigger('channel', 'event', $data);

The .js file:

var pusher = new Pusher('c575a76********db87d', {
    cluster: 'ap2',
    forceTLS: true
});
var channel = pusher.subscribe('channel');
channel.bind('event', function(data) {

    var msg_template = ``; //<-- Just removed template it's simple HTML

    $("ul#messages").append(msg_template);
});
  • 写回答

1条回答 默认 最新

  • doujiao9574 2019-05-15 10:05
    关注

    You need to restrict the message sender from receiving the broadcasted message. How to do so is documented at on the Pusher website.

    First, you need to store the SocketID when connecting to Channels:

    var pusher = new Pusher('APP_KEY');
    var socketId = null;
    pusher.connection.bind('connected', function() {
      socketId = pusher.connection.socket_id;
    });
    

    Once this has been found, you can use it in the event sent to the server:

    data: {
          id: 'some_id',
          updated_value: 'some_value',
          socket_id: socketId // pass socket_id parameter to be used by server
        }
    

    This will then prevent the client with that socketId from receiving the message.

    评论

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)