핸들러 등록

참고

다음은 고급 기능으로, 플러그인의 C++ 소스 코드 편집이 필요합니다.

Strix는 현재 메시지 릴레이 목적의 범용 핸들러가 없습니다. 따라서 이것은 커스텀 구현으로 처리해야 합니다.

다음 샘플 구현에서는 새 함수 두 가지를 StrixBlueprintFunctionLibrary에 추가하고, 라이브러리가 이용하는 StrixNetworkFacade에 추가합니다. 이 함수들은 방과 직접 릴레이 메시지 각각에 핸들러로 블루프린트 함수를 등록하는 것이 목적입니다.

이 메서드는 대부분 변경할 필요가 없습니다. 한 가지 유의할 점은 릴레이 메시지가 서로 타입이 다를 수 있다는 것입니다. 따라서, 아래 마지막 두 함수의 개체 변환 단계는 해당하는 타입으로 바꿔 줘야 합니다.

함수 설정

// StrixBlueprintFunctionLibrary.h

/** Registers a handler to call when a Relay is received
  *  @param handler The callback to call when notification received
  *  @param channelId       The channel to listen on
  */
UFUNCTION(BlueprintCallable, Category = "StrixBPLibrary", meta = (WorldContext = "WorldContextObject"))
    static void RegisterStrixRoomRelayNotificationHandler(const UObject * WorldContextObject, const FStrixRoomRelayNotificationDelegate& handler, int32 channelId);

/** Registers a handler to call when a direct Relay is received
  *  @param handler The callback to call when notification received
  *  @param channelId       The channel to listen on
  */
UFUNCTION(BlueprintCallable, Category = "StrixBPLibrary", meta = (WorldContext = "WorldContextObject"))
    static void RegisterStrixDirectRelayNotificationHandler(const UObject * WorldContextObject, const FStrixDirectRelayNotificationDelegate& handler, int32 channelId);
// StrixBlueprintFunctionLibrary.cpp

void UStrixBlueprintFunctionLibrary::RegisterStrixRoomRelayNotificationHandler(const UObject * WorldContextObject, const FStrixRoomRelayNotificationDelegate& handler, int32 channelId)
{
    // Get the network facade
    UStrixNetworkFacade* strixNetwork = UStrixNetworkFacade::GetInstance(WorldContextObject);

    // Check it exists
    if (!strixNetwork)
    {
        UE_LOG(LogStrix, Log, TEXT("StrixBlueprintFunctionLibrary::RegisterStrixRoomRelayNotificationHandler - Strix network is not initialized."));
        return;
    }

    // Call the register function
    strixNetwork->RegisterRelayNotificationHandler(handler, channelId);
}

void UStrixBlueprintFunctionLibrary::RegisterStrixDirectRelayNotificationHandler(const UObject * WorldContextObject, const FStrixDirectRelayNotificationDelegate& handler, int32 channelId)
{
    // Get the network facade
    UStrixNetworkFacade* strixNetwork = UStrixNetworkFacade::GetInstance(WorldContextObject);

    // Check it exists
    if (!strixNetwork)
    {
        UE_LOG(LogStrix, Log, TEXT("StrixBlueprintFunctionLibrary::RegisterStrixRoomRelayNotificationHandler - Strix network is not initialized."));
        return;
    }

    // Call the register function
    strixNetwork->RegisterDirectRelayNotificationHandler(handler, channelId);
}
// StrixNetworkFacade.h

/** Registers a handler to call when a Relay is received
  *  @param handler The callback to call when notification received
  *  @param channelId       The channel to listen on
  */
void RegisterRelayNotificationHandler(const FStrixRoomRelayNotificationDelegate& handler, int32 channelId);

/** Registers a handler to call when a direct Relay is received
  *  @param handler The callback to call when notification received
  *  @param channelId       The channel to listen on
  */
void RegisterDirectRelayNotificationHandler(const FStrixDirectRelayNotificationDelegate& handler, int32 channelId);

핸들러 등록하기

// StrixNetworkFacade.cpp

// Register Relay handler
void UStrixNetworkFacade::RegisterRelayNotificationHandler(const FStrixRoomRelayNotificationDelegate& handler, int32 channelId)
{

    // Get the room context for this channel. This represents the room connection on the client side.
    auto context = m_roomContextsByChannelId.find(channelId);
    if (context != m_roomContextsByChannelId.end())
    {

        // Get the match room client. This is what the handler will be added to.
        auto matchRoomClient = context->second->GetMatchRoomClient();
        if (matchRoomClient)
        {

            // Add the room Relay notification handler. This takes a lambda function to call when a message is received.
            // The lambda itself takes a NotificationEventArgs<RoomRelayNotificationPtr> type which contains the message.
            matchRoomClient->AddRoomRelayNotifiedHandler([=](strix::client::core::NotificationEventArgs<strix::client::room::message::RoomRelayNotificationPtr> args)
            {
                // Get the message that was sent.
                auto message = args.m_data->GetMessageToRelay();

                // In this case, the message contains an FString.
                // The conversion is undertaken by the ConvertObjectToValue<FString> templated function.
                FString messageText = strix::client::core::util::ObjectConverter::ConvertObjectToValue<FString>(message);

                // Execute the handler.
                // The handler is set in the Blueprint as a function.
                handler.ExecuteIfBound(messageText);
            });
        }
    }
}

void UStrixNetworkFacade::RegisterDirectRelayNotificationHandler(const FStrixDirectRelayNotificationDelegate& handler, int32 channelId)
{

    // Get the room context for this channel. This represents the room connection on the client side.
    auto context = m_roomContextsByChannelId.find(channelId);
    if (context != m_roomContextsByChannelId.end())
    {

        // Get the match room client. This is what the handler will be added to.
        auto matchRoomClient = context->second->GetMatchRoomClient();
        if (matchRoomClient)
        {

            // Add the room Relay notification handler. This takes a lambda function to call when a message is received.
            // The lambda itself takes a NotificationEventArgs<RoomDirectRelayNotificationPtr> type which contains the message.
            matchRoomClient->AddRoomDirectRelayNotifiedHandler([=](strix::client::core::NotificationEventArgs<strix::client::room::message::RoomDirectRelayNotificationPtr> args)
            {

                // Get the message that was sent.
                auto message = args.m_data->GetMessageToRelay();

                // In this case, the message contains an FString.
                // The conversion is undertaken by the ConvertObjectToValue<FString> templated function.
                FString messageText = strix::client::core::util::ObjectConverter::ConvertObjectToValue<FString>(message);

                // Execute the handler.
                // The handler is set in the Blueprint as a function.
                handler.ExecuteIfBound(messageText);
            });
        }
    }
}

용도

../_images/relayhandlers.png