推送一对一的聊天结构

 刘胜良昭桂家贤 发布于 2022-12-07 11:16

presence-channels在Pusher的平台上有点困惑,因为我正在从头开始构建一个聊天应用程序.现在,我知道你们中的一些人已经看到了大量的"实时聊天应用程序"主题,但是,我正在寻找点对点聊天,而不是整个站点的全球性事物.更像是一个Facebook聊天,你可以在那里一对一.

现在,我已经在PubNub的演示中看到了一个例子(名为Babel),但是,这个东西远非我正在寻找的东西,因为我已经在控制台中检查了请求,即使它没有显示,在其他之间发送的消息用户也显示在我的网络请求日志中,因为它在JS中被过滤而不是服务器端,这不是我想要的.

所以,回到主题,我知道频道/私人频道/在线频道功能,我决定这样做:

打开应用程序时,每个用户都会记录到他的private-user_id频道(创建,如果它已经不存在).

同时(在打开应用程序时)user1订阅一个presence-global频道,如果朋友在线,其他人会跟踪.

当别人想送他一个消息,例如user2user1,他赞同private-1其后的JavaScript将处理该事件.

现在,我知道这有什么不对,因为..如果user3会发送消息给user1他订阅,private-user1所以我猜他会看到user2发送消息时触发的事件user1,对吧?或者我弄错了?

我在他们的文档中读到presence频道实际上是一个private频道扩展,所以我现在在想..为什么再使用private频道,然后,我怎么能通知我在线的所有朋友.

但是,在他们的文档中出现了一些其他内容,告诉我channels提供两个重要的事情(其中包括),首先是a way of filtering data第二个,第二个是a way of controlling access.

我怎么应该"过滤数据",因为他们的文档中没有链接,或者更好,你在一对一的聊天中有什么想法.如果我的所有文档都错了,我很抱歉,我看了他们的示例应用程序,但没有一个使用我正在寻找的一对一技术.

我是Pusher和socket连接等的新手,但我已经学会了如何验证,如何创建,检测和处理频道中的事件,我可以创建一个与在线成员进行简单的全局聊天,但是,当涉及到私人频道我对如何为两个用户创建单独的频道感到困惑.

提前致谢 !

1 个回答
  • 私人频道的目的是限制谁可以订阅该频道.所以,您可以:

      使用它可确保只有用户朋友才能订阅更新

      仅将其用于该用户的通知

    在一对一的聊天中,我建议你选择后者(No.2).

    考虑到这一点,我将开始实现一对一的聊天,如下所示:

    论坛

    当用户加入聊天应用程序时,他们都订阅了两个频道:

      private-notifications-<user_id>user_id他们的唯一用户ID leggetter在哪里,例如在我的情况下.此频道用于特定于用户的通知.

      presence-forum对于该论坛中的所有用户.这个问题叫做这个presence-global.

    这是通过以下方式实现的:

    var notifications = pusher.subscribe( 'private-notifications-user_one' );
    var forum = pusher.subscribe( 'presence-forum' );
    

    订阅每个频道后,将进行频道验证过程.

    在论坛中,您可以通过发送和接收消息在presence-forum/ presence-global 在线频道进行一般公共聊天.

    开始一对一聊天

    当一个用户(user_one)希望与另一个用户(user_two)进行私人聊天时,您显然需要UI中的某些内容来触发此操作.说user_one点击旁边的user_two内容表示他们想要一对一的聊天.当发生这种情况时,应该向服务器(权限)发出请求以指示user_one想要启动与user_two† 的私人聊天.

    注意:†如果您为一对一聊天选择了频道命名约定,私人频道认证实际上可以用作私人一对一聊天启动

    当服务器收到此请求时,它可以为此一对一聊天生成唯一的专用通道名称.一种非常简单的方法是通过连接用户ID,例如private-chat-<initiating_user>-<receiving_user>(还有其他考虑因素,例如,您可能希望确保两个用户之间的通道名称始终相同).在我们的简单场景中,频道名称将是private-chat-user_one-user_two.

    然后,服务器可以one-to-one-chat-request在私有通知信道上为每个在有效载荷中传递一对一私人聊天频道名称的用户触发事件.

    // Trigger event on both user channels with one call
    var channels = [ 'private-notifications-user_one', 'private-notifications-user_two' ];
    // Additional event data could also be sent
    // e.g. more info on the initiating user
    var eventData = {
                      'channel_name': 'private-chat-user_one-user_two',
                      'initiated_by': 'user_one'
                      'chat_with'   : 'user_two'
                    };
    pusher.trigger( channels, 'one-to-one-chat-request', eventData );
    

    user_one接收one-to-one-chat-request他们将订阅该eventData.channel_name通道和AUTH过程将发生该通道.

    // A lookup of private chats
    // where the key is the user ID of the current user is chatting with
    var privateChats = {};
    notifications.bind( 'one-to-one-chat-request', function( data ) {
    
      // MY_USER_ID would need to be stored somewhere
      // and in this case the value would be 'user_one'.
      // expectingChatWith should make sure user_one is waiting for
      // a private chat response with the given user
      if( data.initiated_by === MY_USER_ID &&
          expectingChatWith( data.chat_with ) ) {
        startPrivateChat( data.chat_with, data.channel_name );
      }
    
    } );
    
    function startPrivateChat( withUserId, channelName ) {
      privateChats[ withUserId ] = pusher.subscribe( channelName );
    }
    

    user_two接收到one-to-one-chat-request的用户将需要被通知的要求,要么接受拒绝它.如果用户接受,那么客户端代码只是订阅该频道.如果用户拒绝,则应将请求发送到服务器,并在private-notifications-user_one告知他们一对一聊天请求被拒绝时触发事件.这将允许user_one取消订阅私人聊天频道.

    var privateChats = {};
    notifications.bind( 'one-to-one-chat-request', function( data ) {
    
      if( ... ) { ... }
      // has somebody request to chat with this user?
      else if( data.chatWith === MY_USER_ID ) {
        // Prompt the user
        // Note: more user info required
        displayChatPrompt( data );
      }
    
    } );
    
    // callback when the user accepts the chat request
    function accepted( chatUserId, channelName ) {
      startPrivateChat( chatUserId, channelName );
    }
    
    // the user doesn't want to chat
    function declined( chatUserId ) {
      // send info to the server indicating declined request
    }
    

    私密的一对一聊天成功

    通过这两者user_oneuser_two订阅,private-chat-user_one-user_two他们可以在频道上触发事件并参与他们的私人一对一聊天.

    2022-12-11 02:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有