room_broadcast

room_broadcast Sample

Overview

The room_broadcast sample is a sample program that uses the Room and P2P functionalities of the Diarkis server. Two users connect to the same Room, communicate via relay through the Room, and then switch to P2P communication. The room_broadcast sample allows you to verify the following features:

  • Creating a Room

  • Joining a Room

  • Sending messages to Room members

  • Initiating P2P

  • Sending and receiving messages via P2P

Starting the Server in a Local Environment

Follow the tutorial to start the server used in the sample and launch the Diarkis server in your local environment.

Launching Diarkis Server in a Local Environment

Explanation of the room_broadcast Sample

  1. Initialize the Diarkis runtime and Diarkis Module and connect to the Diarkis server. For details, refer to General flow of using the Diarkis module.

  2. Initialize each functionality. In this sample, we use the Room module and P2P module, so initialize those functions using the Diarkis Module.

    // diarkis =  Diarkis::DiarkisAllocShared<DiarkisInterface>(uid); 
    ...
    // Setup P2P module
    diarkis->SetupP2P();
    
    // Setup Room module
    diarkis->SetupRoom(false);
    
  3. Join a Room using RandomJoin. DiarkisRoomBase::RandomJoinRoom() joins an existing Room if available, or creates a new Room if none is available. After sending the request to the server with DiarkisRoomBase::RandomJoinRoom(), check the status with DiarkisRoomBase::IsJoin() until the join is complete.

    diarkis->RandomJoinRoom(10, 60, 0, true);
    
    // Wait for DiarkisRoomBase::OnRoomJoin
    while (diarkis->GetRoomBase()->IsJoin() == false) {
        std::this_thread::sleep_for(std::chrono::milliseconds(30));
    }
  4. Wait until the expected number of participants have joined the Room. In this sample, wait until 2 users have joined the same Room. You can request a list of the members in the Room by calling DiarkisRoomBase::SendGetMemberIDs(). The results can be obtained with DiarkisRoomBase::GetRoomMembers().

    while (1)
    {
        Diarkis::StdVector<Diarkis::StdString> members;
    
        diarkis->GetRoomBase()->SendGetMemberIDs();
        diarkis->GetRoomBase()->GetRoomMembers(members);
    
        if (members.size() == NumPeer)
        {
            break;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
  5. Send a message to all users in the Room using the Room's Broadcast.

    diarkis->GetRoomBase()->SendBroadcastToRoom(buff, bReliable);
  6. The Room owner executes DiarkisRoomBase::SendStartP2PSync() to notify the server to start P2P connections and obtain the connection address list of the members in the Room.

    if (Diarkis::StdString(uid.c_str()) == diarkis->GetRoomBase()->GetOwnerUID())
    {
        // Get a list of addresses for each member's connection
        diarkis->GetRoomBase()->SendStartP2PSync();
    }
    
  7. When the Room owner executes DiarkisRoomBase::SendStartP2PSync(), P2P connection information is notified to all users in the Room via DiarkisRoomBase::OnStartP2PSync(), which triggers the start of hole punching.

  8. After successful hole punching, communication via P2P begins.

    diarkis->GetP2PBase()->SendBroadcast(buff, RudpType::UNRELIABLE_UNORDERED);
  9. Execute DiarkisRoomBase::SendLeaveRoom() to request to leave the Room to the server. In this sample, wait until leaving is complete using DiarkisRoomBase::IsLeave().

    // Leave room
    diarkis->SendLeaveRoom();
    
    // Wait for DiarkisRoomBase::OnRoomLeave
    while (diarkis->GetRoomBase()->IsLeave() == false) {
        std::this_thread::sleep_for(std::chrono::milliseconds(30));
    }
  10. Finalize For details, refer to General flow of using the Diarkis module.

Notices

  • To successfully perform hole punching, the client performing P2P must exchange addresses while in the same Room.

  • Prepare multiple clients and run them simultaneously to check the operation.

最終更新