LuckyWorldV2/Source/Luckyrobots/Private/GameModes/LuckyRobotsGameState.cpp

49 lines
1.5 KiB
C++
Raw Normal View History

2025-03-30 11:46:50 +08:00
// Fill out your copyright notice in the Description page of Project Settings.
2025-04-07 10:28:47 +08:00
#include "GameModes/LuckyRobotsGameState.h"
2025-04-01 11:04:11 +08:00
#include "SocketIOClientComponent.h"
2025-04-07 10:28:47 +08:00
#include "FunctionLibraries/LuckyRobotsFunctionLibrary.h"
#include "Core/LuckyRobotsGameInstance.h"
2025-04-09 15:18:16 +08:00
#include "SIOJLibrary.h"
2025-03-30 11:46:50 +08:00
2025-04-01 11:04:11 +08:00
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)
{
2025-04-07 11:32:45 +08:00
if (SocketIOClientComponent && SocketIOClientComponent->bIsConnected)
2025-04-01 11:04:11 +08:00
{
2025-04-07 11:32:45 +08:00
USIOJsonValue* SIOJsonValue = USIOJsonValue::ConstructJsonValueString(this, SendValue);
SocketIOClientComponent->Emit(TEXT("message"), SIOJsonValue);
2025-04-01 11:04:11 +08:00
}
}
void ALuckyRobotsGameState::DoSocketOnConnect(FString SocketId, FString SessionId, bool IsReconnection)
{
2025-04-07 11:32:45 +08:00
if (SocketIOClientComponent && SocketIOClientComponent->bIsConnected)
2025-04-01 11:04:11 +08:00
{
2025-04-07 11:32:45 +08:00
SocketIOClientComponent->BindEventToGenericEvent(TEXT("response"));
2025-04-01 11:04:11 +08:00
}
}
void ALuckyRobotsGameState::DoSocketOnGenericEvent(FString EventName, USIOJsonValue* EventData)
{
2025-04-07 11:32:45 +08:00
if (ULuckyRobotsGameInstance* GameInstance = ULuckyRobotsFunctionLibrary::GetLuckyRobotsGameInstance(this))
2025-04-01 11:04:11 +08:00
{
2025-04-09 15:18:16 +08:00
GameInstance->OnMessageDispatched.Broadcast(EventName);
GameInstance->GetMessageParse(USIOJLibrary::Conv_SIOJsonValueToString(EventData));
2025-04-01 11:04:11 +08:00
}
2025-04-07 11:32:45 +08:00
}