我presence-channels
在Pusher的平台上有点困惑,因为我正在从头开始构建一个聊天应用程序.现在,我知道你们中的一些人已经看到了大量的"实时聊天应用程序"主题,但是,我正在寻找点对点聊天,而不是整个站点的全球性事物.更像是一个Facebook聊天,你可以在那里一对一.
现在,我已经在PubNub的演示中看到了一个例子(名为Babel),但是,这个东西远非我正在寻找的东西,因为我已经在控制台中检查了请求,即使它没有显示,在其他之间发送的消息用户也显示在我的网络请求日志中,因为它在JS中被过滤而不是服务器端,这不是我想要的.
所以,回到主题,我知道频道/私人频道/在线频道功能,我决定这样做:
打开应用程序时,每个用户都会记录到他的private-user_id
频道(创建,如果它已经不存在).
同时(在打开应用程序时)user1
订阅一个presence-global
频道,如果朋友在线,其他人会跟踪.
当别人想送他一个消息,例如user2
到user1
,他赞同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连接等的新手,但我已经学会了如何验证,如何创建,检测和处理频道中的事件,我可以创建一个与在线成员进行简单的全局聊天,但是,当涉及到私人频道我对如何为两个用户创建单独的频道感到困惑.
提前致谢 !
私人频道的目的是限制谁可以订阅该频道.所以,您可以:
使用它可确保只有用户朋友才能订阅更新
仅将其用于该用户的通知
在一对一的聊天中,我建议你选择后者(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_one
并user_two
订阅,private-chat-user_one-user_two
他们可以在频道上触发事件并参与他们的私人一对一聊天.