LuckyWorldV2/Source/Luckyrobots/Private/GameModes/LuckyRobotsGameState.cpp
martinluckyrobots 4d4026d9ad Optimize code to unreal style
2025-04-07 11:32:45 +08:00

47 lines
1.4 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "GameModes/LuckyRobotsGameState.h"
#include "SocketIOClientComponent.h"
#include "FunctionLibraries/LuckyRobotsFunctionLibrary.h"
#include "Core/LuckyRobotsGameInstance.h"
ALuckyRobotsGameState::ALuckyRobotsGameState()
{
SocketIOClientComponent = CreateDefaultSubobject<USocketIOClientComponent>(TEXT("SocketIOClientComponent"));
}
void ALuckyRobotsGameState::BeginPlay()
{
Super::BeginPlay();
if (SocketIOClientComponent)
{
SocketIOClientComponent->Connect(L"http://localhost:3000/");
}
}
void ALuckyRobotsGameState::DoSendMessage(FString SendValue)
{
if (SocketIOClientComponent && SocketIOClientComponent->bIsConnected)
{
USIOJsonValue* SIOJsonValue = USIOJsonValue::ConstructJsonValueString(this, SendValue);
SocketIOClientComponent->Emit(TEXT("message"), SIOJsonValue);
}
}
void ALuckyRobotsGameState::DoSocketOnConnect(FString SocketId, FString SessionId, bool IsReconnection)
{
if (SocketIOClientComponent && SocketIOClientComponent->bIsConnected)
{
SocketIOClientComponent->BindEventToGenericEvent(TEXT("response"));
}
}
void ALuckyRobotsGameState::DoSocketOnGenericEvent(FString EventName, USIOJsonValue* EventData)
{
if (ULuckyRobotsGameInstance* GameInstance = ULuckyRobotsFunctionLibrary::GetLuckyRobotsGameInstance(this))
{
GameInstance->DoGetDispatch(EventName, EventData);
}
}