Android - 使用 parse 和 pubnub 在随机聊天应用程序中正确配对和连接两个用户 [英] Android - correctly pairing and connecting two users in a random chat app using parse and pubnub

查看:17
本文介绍了Android - 使用 parse 和 pubnub 在随机聊天应用程序中正确配对和连接两个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个随机聊天应用程序,用户按下一个按钮并与另一个用户配对,然后他们就可以聊天了.
我计划使用 Parse 进行用户控制和文件存储,并使用 pubnub 进行聊天.

我的问题是,配对和连接两个用户并将他们放在一起聊天的正确方法是什么?

假设 user1 按下搜索按钮开始搜索另一个用户,然后,用户 1 在解析数据库中的状态更新为正在搜索",然后应用程序会在数据库中查询另一个状态也是正在搜索"的用户.查询返回 user2,并根据两个用户名的组合创建一个新的聊天频道(例如,Pubnub 聊天频道:'user1user2').
然后将 User1 放入此聊天频道并启动聊天活动.这就是我感到困惑的地方,我的应用程序如何知道它需要将 user2 放入与 user1 的聊天中?因为根据我的想法,user2 也按下了搜索按钮来查询另一个用户的解析数据库,因此他们可以与另一个随机用户而不是 user1 进行聊天.

任何有关实现此目标的正确方法的帮助将不胜感激,谢谢.

I'm currently creating a random chat application where the user presses a button and is paired with another user and then they can chat.
I plan on using Parse for user control and file storage, and pubnub for chat.

My question is, what would be the correct way to pair and connect two users and put them into a chat together?

Lets say user1 presses the search button to start searching for another user, user1's status in the parse database is then updated to 'searching' and the app then queries the database for another user who's status is also 'searching'. the query returns user2 and a new chat channel is created from the two users names combined together (e.g. Pubnub Chat Channel: 'user1user2').
User1 is then put into this chat channel and the chat activity is started. This is where I get confused, how will my app then know that it needs to put user2 into a chat with user1? Because from my thinking, user2 has also pressed the search button to query the parse database for another user and therefore they could be put into a chat with another random user and not user1.

Any help on the correct method to achieve this would be really appreciated, thanks.

推荐答案

这是一个很酷的用例.以下是有关如何开始使用此设计的一些见解.

This is a cool use case. Here's some insight into how to get started with this design.

  • 每个用户都将被分配一个唯一的频道名称和一个唯一的频道组,例如分别为 ch_user123cg_user123.
  • 将有三个服务器管理频道组,分别命名为cg_idlecg_searchingcg_active.此频道组将包含未积极参与聊天且未搜索聊天伙伴的用户的所有唯一用户频道名称.
  • Each user will be assigned a unique channel name and a unique channel group, for example, ch_user123 and cg_user123, respectively.
  • There will be three server managed channel groups named cg_idle, cg_searching, cg_active. This channel group will contain all the unique user channel names of users that are not actively engaged in a chat and not searching for a chat partner.

当用户登录(成功)时,您的服务器会将该用户的唯一频道添加到频道组idle 和用户的唯一频道组(IOW - add ch_user123> 到 cg_user123cg_idle

When a user logs in (successfully), your server will add that user's unique channel to the channel group idle and to the user's unique channel group (IOW - add ch_user123 to cg_user123 and to cg_idle

当用户点击搜索聊天伙伴按钮时,您的服务器应用将

When a user clicks the search for chat partner button, your server app will

  1. cg_idle 频道组中删除他们的用户唯一频道
  2. 将他们的用户唯一频道添加到 cg_searching 频道组
  3. cg_idle
  4. list_channels 获取聊天伙伴候选列表
  5. cg_idle 候选列表中选择一个用户频道
  6. 检查以确保所选候选人仍然空闲
  7. 如果用户不再空闲,则他们处于活动状态或正在搜索 - 从空闲列表中选择另一个用户(需要再次列出 cg_idle 的频道以获取更新列表)- IOW,去返回步骤 4
  8. 如果用户仍然空闲,从 cg_idle 中删除该用户的频道并将其添加到 cg_active
  9. cg_searching中移除搜索用户的频道并将其添加到cg_active
  10. 将新生成的频道名称(您可以使用 UUID API 生成 UUID 格式名称)添加到两个用户的唯一频道组.例如,将新频道名称1234-5678-9ABC 添加到cg_user123cg_user456.这两个用户没有订阅同一个频道来开始彼此的聊天冒险.
  11. 您的服务器现在可以向这个新频道发布消息,向用户介绍彼此(您的客户端应用程序可以显示头像、用户信息,甚至 如果您这样做,请启动视频流).
  12. 如果任一用户点击离开按钮,两个用户的唯一频道将从 cg_active 中删除,添加到 cg_idle,并且共享聊天频道将从两个用户的独特的渠道组.
  1. remove their user unique channel from the cg_idle channel group
  2. add their user unique channel to the cg_searching channel group
  3. list_channels of the cg_idle to get a list of chat partner candidates
  4. select a user channel from the list of cg_idle candidates
  5. check to make sure the selected candidate is still idle
  6. if the user is no longer idle then they are active or searching - pick another user from the idle list (need to list the channels of cg_idle again to get an updated list) - IOW, go back to step 4
  7. if user still idle, remove that user's channel from cg_idle and add it to cg_active
  8. remove the searching user's channel from cg_searching and add it to cg_active
  9. add a newly generated channel name (you can use the UUID API to generate a UUID format name) to both users' unique channel groups. For example, add new channel name 1234-5678-9ABC to cg_user123 and cg_user456. These two users are not subscribe to the same channel to begin their chatting adventure with each other.
  10. your server can now publish a message to this new channel introducing the users to each other (your client app can display avatar, user info, or even start the video stream if you are doing that).
  11. if either user clicks the leave button, both users' unique channels are removed from cg_active, added to cg_idle, and the share chat channel is removed from both users' unique channel groups.

我可以想到上面需要解决的几个细节和功能以及您的服务可以控制的竞争条件,但这应该揭示您如何使用频道组来控制用户的状态以及创建用户名目录.

I can think of several details and features that would need to be addressed above and race conditions that your service would have control but this should expose how you can use channel groups to control the state of the users and a way of creating a name directory of users.

这篇关于Android - 使用 parse 和 pubnub 在随机聊天应用程序中正确配对和连接两个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆