gpt4 book ai didi

javascript - Pusher 一对一聊天结构

转载 作者:数据小太阳 更新时间:2023-10-29 04:26:14 24 4
gpt4 key购买 nike

我对 Pusher 平台中的 presence-channels 有点困惑,因为我正在从头构建聊天应用程序。现在,我知道你们中的一些人已经看到了大量的“实时聊天应用程序”主题,但是,我正在寻找点对点聊天而不是站点范围内的全局性话题。更像是 Facebook 聊天,您可以在那里一对一聊天。

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

所以,回到主题,我知道 channel /私有(private) channel /存在 channel 功能,我决定这样做:

  • 打开应用程序时,每个用户都订阅他的 private-user_id channel (创建,如果它尚不存在)。

  • 同时(打开应用程序时)user1 订阅了一个 presence-global channel ,如果 friend 在线,其他人会在该 channel 上进行跟踪。

  • 当其他人想给他发消息时,例如user2user1,他订阅了private-1,之后javascript会处理事件。

现在,我知道这有问题,因为..如果 user3 会发送消息给 user1 他会订阅 private-user1 所以我猜他在向 user1 发送消息时也会看到 user2 触发的事件,对吗?还是我弄错了?

我在他们的文档中读到presence channel 实际上是一个private channel 扩展,所以我现在在想..为什么不再使用 private channel ,然后,我如何通知我所有的 friend 我在线。

但是随后,他们的文档 中出现了其他内容,告诉我channels 提供了两个重要的东西(除其他外),首先是a过滤数据的方式,第二种是控制访问的方式

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

我是 Pusher 和套接字连接等方面的新手,但我已经学会了如何进行身份验证,如何创建、检测和处理 channel 中的事件,并且我可以与在线成员创建简单的全局聊天,但是,当谈到私有(private) channel ,我对如何为两个用户创建单独的 channel 感到很困惑。

提前致谢!

最佳答案

目的private channels是限制谁可以订阅该 channel 。因此,您可以:

  1. 用它来确保只有用户的 friend 可以订阅更新
  2. 仅用于该用户的通知

在一对一聊天中,我建议您选择后者(第 2 条)。

考虑到这一点,我打算按如下方式实现一对一聊天:

论坛

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

  1. private-notifications-<user_id>其中 user_id是他们唯一的用户 ID,例如leggetter就我而言。此 channel 用于特定于用户的通知。
  2. presence-forum对于该论坛中的所有用户。这个问题叫做 presence-global .

实现方式如下:

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

订阅每个 channel 后 channel authentication过程将发生。

在论坛中,您可以在 presence-forum 上进行一般公开聊天/presence-global presence channel通过发送和接收消息。

开始一对一聊天

当一个用户 ( user_one ) 想要与另一个用户 ( user_two ) 进行私有(private)聊天时,您显然需要 UI 中的某些内容来触发此操作。说 user_one单击 user_two 旁边的内容这表明他们想要一对一的聊天。当发生这种情况时,应向服务器(权威机构)发出请求以指示 user_one想发起与 user_two 的私聊†.

注意:† 如果您为一对一聊天选择 channel 命名约定,则私有(private) channel 身份验证实际上可以用作私有(private)一对一聊天启动

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

然后服务器可以触发 one-to-one-chat-request每个用户的私有(private)通知 channel 上的事件在有效负载中传递一对一的私有(private)聊天 channel 名称。

// 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 channel 和 auth process将在该 channel 发生。

// 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用户将需要收到有关请求的通知,并接受拒绝。如果用户接受,则客户端代码只需订阅该 channel 。如果用户拒绝,则应向服务器发送请求并在 private-notifications-user_one 上触发事件。告诉他们他们的一对一聊天请求被拒绝了。这将允许 user_one取消订阅私有(private)聊天 channel 。

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他们可以在 channel 上触发事件并参与他们的私有(private)一对一聊天

关于javascript - Pusher 一对一聊天结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27375125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com