LogoLogo
English
English
  • Diarkis Help Center
  • Overview of Diarkis
  • Getting Started
    • Diarkis Server Template
    • Diarkis Client SDK
    • Tutorials
      • 1. Launch Diarkis Server in Local Environment
      • 2. Perform Connectivity Check with Test Client
      • 3. Implement Custom Command
      • Connect to Server from Diarkis Client
    • Samples
  • Diarkis Modules
    • Room Module
      • Set Up Room Module on Server
      • Room Sample
        • room_broadcast
      • Utilizing Room Module from Client
      • Additional Features of Room
    • MatchMaker Module
      • Set Up MatchMaker Module on Server
    • Field Module
      • Set Up Field Module on Server
    • P2P Module
      • Set Up P2P Module on Server
      • P2P Sample
    • DM (Direct Message) Module
      • Set Up DM Module on Server
    • Notifier Module
      • Set Up Notifier Module on Server
    • Session Module
      • Set Up Session Module on Server
    • Group Module
      • Set Up Group Module on Server
  • Diarkis Server
    • Launch Diarkis Server in Cloud Environment
      • AWS
    • Launch Diarkis Server on Windows Environment
    • MARS Server
    • UDP Server
    • TCP Server
    • HTTP Server
    • Metrics API
    • Inter-server Communication - Mesh
  • Diarkis Client
    • Runtime Library
      • Diarkis RUDP
    • Diarkis Module
      • Initialization and Termination of Diarkis Module
      • Customization of Diarkis Module
      • Logging System of Diarkis Module
      • Migration
      • Threads of Diarkis
    • Samples
      • C++
        • room_broadcast
        • directmessage_simple
        • group_sample
        • matching_and_turn
        • matchmaker_ticket
        • p2p_rudp_sample
        • session_simple
      • Unreal Engine Plugin
        • FieldWalker
      • Unity Plugin
        • FieldWalker
          • HowToReplicatePosition.md
  • Diarkis Tools
    • Diarkis CLI
      • Procedures to Switch to Diarkis CLI v3
  • References
    • API Reference
    • Release Notes
      • v1.0
      • v1.0.1
      • v1.0.2
      • v1.0.3
      • v1.0.4
      • v1.0.5
      • v1.0.6
  • Support
    • License and Billing
Powered by GitBook
On this page
  • room_broadcast Sample
  • Overview
  • Starting the Server in Your Local Environment
  • Explanation of the room_broadcast Sample
  • Notes

Was this helpful?

  1. Diarkis Modules
  2. Room Module
  3. Room Sample

room_broadcast

PreviousRoom SampleNextUtilizing Room Module from Client

Last updated 2 months ago

Was this helpful?

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 .

  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 .

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.

Overall Flow for Using Diarkis Module
Overall Flow for Using Diarkis Module