room_broadcast

room_broadcast Sample

Overview

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

  • Creation of a Room

  • Participation in a Room

  • Sending messages to Room members

  • Initiation of P2P

  • Sending and receiving messages through P2P

Starting the Server in a Local Environment

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

1. Launch Diarkis Server Locally

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 process of utilizing the 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 of the P2P module
    diarkis->SetupP2P();
    
    // Setup of the Room module
    diarkis->SetupRoom(false);
  3. Enter a Room using RandomJoin. If a joinable Room already exists, DiarkisRoomBase::RandomJoinRoom() will join it; otherwise, it will create a new Room. After sending a request to the server with DiarkisRoomBase::RandomJoinRoom(), check the state until the Room join is complete using DiarkisRoomBase::IsJoin().

    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 people have joined the Room. In this sample, wait until 2 users have joined the same Room. You can request the list of members participating in the Room by calling DiarkisRoomBase::SendGetMemberIDs(), and the result can be retrieved 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 participating 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 address list of the connection destinations of the members participating in the Room.

    if (Diarkis::StdString(uid.c_str()) == diarkis->GetRoomBase()->GetOwnerUID())
    {
        // Obtain the address list of each member's connection destination
        diarkis->GetRoomBase()->SendStartP2PSync();
    }
  7. When the Room owner executes DiarkisRoomBase::SendStartP2PSync(), P2P connection information is notified to all users in the Room through DiarkisRoomBase::OnStartP2PSync(), triggering the start of hole punching.

  8. After successful hole punching, communicate via P2P.

    diarkis->GetP2PBase()->SendBroadcast(buff, RudpType::UNRELIABLE_UNORDERED);
  9. Execute DiarkisRoomBase::SendLeaveRoom() to request leaving 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 process For more details, refer to Overall process of utilizing the Diarkis module.

Notes

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

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

Last updated