Android - correctly pairing and connecting two use

2019-05-31 10:23发布

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.

1条回答
Animai°情兽
2楼-- · 2019-05-31 11:00

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

Setup

  • 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.

User Login

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

Search of Chat Partner

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

  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.

查看更多
登录 后发表回答