LuckyRobotsUnreal/Source/Luckyrobots/Private/LuckyRobotsGameState.cpp
martinluckyrobots 8e1c017515 Optimize gamemode and gamestate code
2025-04-01 11:04:11 +08:00

55 lines
1.4 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "LuckyRobotsGameState.h"
#include "SocketIOClientComponent.h"
#include "LuckyRobotsFunctionLibrary.h"
#include "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)
{
if (SocketIOClientComponent->bIsConnected)
{
USIOJsonValue* SIOJsonValue = USIOJsonValue::ConstructJsonValueString(this, SendValue);
SocketIOClientComponent->Emit("message", SIOJsonValue);
}
}
}
void ALuckyRobotsGameState::DoSocketOnConnect(FString SocketId, FString SessionId, bool IsReconnection)
{
if (SocketIOClientComponent)
{
if (SocketIOClientComponent->bIsConnected)
{
SocketIOClientComponent->BindEventToGenericEvent("response");
}
}
}
void ALuckyRobotsGameState::DoSocketOnGenericEvent(FString EventName, USIOJsonValue* EventData)
{
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = ULuckyRobotsFunctionLibrary::GetLuckyRobotsGameInstance(this);
if (LuckyRobotsGameInstance)
{
LuckyRobotsGameInstance->DoGetDispatch(EventName, EventData);
}
}