Programmer's hard core romance - girlfriend's exclusive chat room (with source code)

Everyone says that it's good to find a program ape to be a boyfriend. More money, less talk and over early.

Um... It is estimated that Xiaobian will be wildly arrested and beaten by the company's program ape compatriots after publishing the article.

Although in daily life, most of the labels of program apes are rigid, dull and do not understand romance. Plaid shirts, jeans and black framed glasses forgive Coding all day. Even if it is surrounded by red wine, candles and flowers, an alarm should be immediately taken out of the computer for processing.

However, when it comes to the romance of program apes, it is natural to force grid. Code confession is a necessary skill for a professional program ape.

For example, this sentence:

——Meet you forever, lose you like a year.

while True:
    if meet_you: life++
    else: life--

Xiao Liu is a programmer of a company. He has new tricks every time. His girlfriend's birthday plan this year is different.

Think of the hot clubhouse at the beginning of the year. My girlfriend begged all the friends in the circle of friends, but it was still hard to find one yard.

So, a strange inspiration emerged - to be a chat room to send her!!!

You can not only talk quietly, but also let her chat with her friends inside. It's really killing two birds with one stone! Programmers, what they pursue is a sense of atmosphere!

I learned that Jigou has also made a chat room product recently, so I log in to Jigou's official website and try to build a chat room by myself. Here are the detailed steps. Let's have a look with me!

1, Start preparing

First of all, before we start to implement the demo of the chat room scenario, we need to make some preparations before development:

1. Development environment preparation: (take android as an example)

  • Android Studio 2.1 or above
  • Android SDK 25, Android SDK build tools 25.0.2, Android SDK platform tools 25. X.x or above
  • Android 4.1 or above and supports audio and video
  • The Android device is connected to the Internet

2. Account registration:

After the development environment is set up, we need to register an account on the official website of ZEGO, enter the management console, create applications and obtain AppId and AppSign for development.

The specific application creation process is detailed in this article: console creation project process Console project management - Developer Center - ZEGO instant Technology

2, Introduction to implementation process

Recently, in order to enable developers to quickly realize the chat room business, ZEGO instant technology has specially encapsulated the chat room SDK (ZIMChatRoom) based on Express SDK and ZIM SDK, so we can quickly access the business functions required by the chat room and achieve out of the box.

At the same time, the chat room SDK is fully open source, so we can also customize and modify the source code based on the actual business logic, which can be said to be very considerate!!!

Here, you need to obtain the sample source code of ZIMChatRoom. Download address:

Next, we will implement the core business logic of the chat room based on ZIMChatRoom.

1. Initialization

The first step is initialization, and the App configuration required for initialization needs to be obtained by contacting ZEGO technical support.

Long appID = 214124124L;  // Contact ZEGO sales or pre-sales Engineer for configuration
String appSign = "xxx";   // Contact ZEGO sales or pre-sales Engineer for configuration
// The last parameter, application, refers to the application object running ZIMChatRoom
ZIMChatRoom.getInstance().init(appID, appSign,this);
// Register callback events
ZIMChatRoom.getInstance().setZIMChatRoomEventHandler(new ZIMChatRoomEventHandler() {

2. User login

When logging in to IM services, we need to log in using Token authentication. Please refer to using Token authentication for details of obtaining the authentication Token of ZIM( Android instant messaging uses Token for login authentication - Developer Center - ZEGO instant Technology).

The user login code is as follows:

ZIMChatRoomUserInfo user  = new ZIMChatRoomUserInfo();    // Set related properties of user    
String token = "xxx";  // Need to get 
ZIMChatRoom.getInstance().login(user, token, new LoginCallback() {             
    public void onLogined(ZIMChatRoomErrorCode error) {    
    // Login callback    

3. Create a chat room

After completing the previous process, you can start to create a room: (a RTC Token acquisition process should be added here: Developer Center - instant Technology)

ZIMChatRoomInfo roomInfo = new ZIMChatRoomInfo();
// Set various properties of the room as needed
String rtcToken = "xxx";  // Need to get RTC Token
ZIMChatRoom.getInstance().createChatRoom(roomInfo, rtcToken, new CreateChatRoomCallback() {
   public void onChatRoomCreated(ZIMChatRoomErrorCode error) {
       // Create room situation

4. Audience participation

After the room is built, we can join the room first: (here we need to add an RTC Token acquisition process: Developer Center - instant Technology)

String roomID = "ROOM_ID";
String rtcToken = "xxx";  // Need to get RTC Token
ZIMChatRoom.getInstance().joinChatRoom(roomID,rtcToken,new JoinChatRoomCallback() {
   public void onChatRoomJoined(ZIMChatRoomErrorCode error) {
       // Join the room

5. Audience on Wheat

On the microphone. After successful on the microphone, the SDK will automatically collect the microphone sound and push the stream:

int seatIndex = 2;  // Wheat level subscript position 
ZIMChatRoom.getInstance().enterSeat(seatIndex, new EnterSeatCallback(){    
    public  void onSeatEntered(ZIMChatRoomErrorCode error){
        // Processing wheat results

Note: make sure that the microphone permission is turned on before loading the microphone

6. Invite friends

In addition to the audience's active wheat, the homeowner can also take the initiative to invite friends in the room to wheat:

String toUserID = "USERID_OF_INVITED"; // userID of the party receiving the invitation
ZIMChatRoom.getInstance().sendInvitation(toUserID, new SendInvitationStatusCallback() {
    public void onInvitationStatusSent(ZIMChatRoomErrorCode errorCode) {
    // Send invitation result callback

When another user receives the invitation request, it will receive this callback notification:

// Receive invitation to wheat processing
ZIMChatRoom.getInstance().setZIMChatRoomEventHandler(new ZIMChatRoomEventHandler() {
   public void onReceiveInvitation(String fromUserID) {
       // After receiving the invitation information, the user can actively call the upload operation

7. Chat

Well, after completing the above steps, we can start voice chat. In addition to the basic voice chat that can be realized in the language chat room, users in the room can also carry out public screen text chat:

ZIMChatRoom.getInstance().sendRoomMessage("YOUR_MESSAGE", new SendRoomMessageCallback() {
   public void onRoomMessageSent(ZIMChatRoomErrorCode error) {
       // Results of sending room chat messages

8. Give gifts

Of course, you can also give gifts to your girlfriend:

int giftType = 1;  // The gift type can be customized by the developer, and the SDK is only used for transparent transmission
ArrayList<String> toUserIDList = new ArrayList<>();  // The list of user IDs for receiving gifts requires developers to add the user ID for receiving gifts
ZIMChatRoom.getInstance().sendGiftMessage(giftType, toUserIDList, new SendGiftMessageCallback() {
   public void onGiftMessageSent(ZIMChatRoomErrorCode error, List<String> sendFailToUsers) {
   // Receive the results of sending gifts and the list of users who failed to receive gifts

In this way, the user receiving the gift will receive the onReceiveGiftMessage notification, while other users in the room will receive the onReceiveGiftBroadcastMessage notification, so as to display some cool words and special effects on the UI and remind the girlfriend in the room that "your boyfriend has sent you a gift" ~

//Receive gifts
ZIMChatRoom.getInstance().setZIMChatRoomEventHandler(new ZIMChatRoomEventHandler() {
   public void onReceiveGiftMessage(int giftType, String fromUserID) {
   // Operate on receiving gifts
   public void onReceiveGiftBroadcastMessage(List<String> toUSerIDList, int giftType) {
   // Broadcast operation on received gifts

9. Lower wheat

When you no longer need to chat on the Mac, call the next Mac to stop sending audio data immediately:

// Wheat feeding operation
ZIMChatRoom.getInstance().leaveSeat(new LeaveSeatCallback(){
   public void onSeatLeft(ZIMChatRoomErrorCode error){
       // Operate under wheat conditions

Through the above steps, you have basically completed the construction of the chat room and other operations. The implementation process is also very clear at a glance. You can't wait to invite your girlfriend to connect with Mai!!!

3, Effect presentation (schematic diagram)

This is the complete construction process. Some screenshots of the demo are saved and shown to you:



4, Conclusion

There are many ways of romance. Who says programmers don't understand romance?

Romance is not only flowers and love words, but also programmers' handwritten code.

I'm not afraid to fall in love with programmers. I'm afraid programmers understand romance.

After all, they are romantic and no one can resist them.

Your girlfriend's exclusive chat room, have you learned it? Hurry up and build one yourself. Isn't it beautiful?

Finally, there is a wave of double 11 benefits. From 9.9 yuan, the best practice documents of the chat room, Demo + source code + video commentary, and an additional 10000 minutes. Code scanning consultation 👇 (of course, if you encounter any technical problems during the construction process, you can also consult through this wechat.)


Click the link to see more details: Double 11 · annual reserve price - ZEGO instant Technology

Tags: Android

Posted on Tue, 16 Nov 2021 23:26:11 -0500 by mchannel