room_broadcast

room_broadcast Sample

Overview

The room_broadcast sample is a demonstration program utilizing the Room and P2P features of the Diarkis server. It allows two users to connect to the same Room, conduct relay communication via the Room, then establish a P2P connection for direct communication. The room_broadcast sample showcases the following features:

  • Room creation

  • Joining a Room

  • Sending messages to Room members

  • Initiating P2P

  • Sending and receiving messages via P2P

Starting the Server in Your Local Environment

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

1. Launch Diarkis Server in Local Environment

Explanation of the room_broadcast Sample

  1. Initialize the Diarkis runtime and Diarkis Module and connect to the Diarkis server. For more details, refer to Overall Flow for Using Diarkis Module.

  2. Initialize the necessary functions. Since this sample uses the Room and P2P modules, 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() will join an existing Room if available, or create a new one if not. After sending a request to the server via DiarkisRoomBase::RandomJoinRoom(), check the status with DiarkisRoomBase::IsJoin() until the Room 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 join the Room. In this sample, wait until 2 users join the same Room. You can request the list of 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. Use Room Broadcast to send a message to all users in the Room.

    diarkis->GetRoomBase()->SendBroadcastToRoom(buff, bReliable);
  6. The Room owner executes DiarkisRoomBase::SendStartP2PSync() to notify the server of the initiation of P2P connections and to retrieve a list of address connections for participants in the Room.

    if (Diarkis::StdString(uid.c_str()) == diarkis->GetRoomBase()->GetOwnerUID())
    {
        // Retrieve address list of 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(), triggering the start of hole punching.

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

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

    // Leave the Room
    diarkis->SendLeaveRoom();
    
    // Wait for DiarkisRoomBase::OnRoomLeave
    while (diarkis->GetRoomBase()->IsLeave() == false) {
        std::this_thread::sleep_for(std::chrono::milliseconds(30));
    }
  10. Termination For more details, refer to Overall Flow for Using Diarkis Module.

Notes

  • To successfully achieve P2P hole punching, the clients must be in the same Room and exchange addresses.

  • Please prepare multiple clients and execute them simultaneously to verify operation.

Last updated

Was this helpful?