177 lines
6.3 KiB
C++
177 lines
6.3 KiB
C++
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|||
|
|
|
|||
|
|
#include "Subsystems/DTFluxNetworkSubsystem.h"
|
|||
|
|
|
|||
|
|
#include "DTFluxCoreModule.h"
|
|||
|
|
#include "DTFluxNetworkModule.h"
|
|||
|
|
#include "DTFluxNetworkSettings.h"
|
|||
|
|
#include "Clients/DTFluxHttpClient.h"
|
|||
|
|
#include "Clients/DTFluxWebSocketClient.h"
|
|||
|
|
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::Connect()
|
|||
|
|
{
|
|||
|
|
WsClient->SetAddress(ConstructWsAddress(WsSettings.Address, WsSettings.Path, WsSettings.Port));
|
|||
|
|
WsClient->Connect();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::Initialize(FSubsystemCollectionBase& Collection)
|
|||
|
|
{
|
|||
|
|
Super::Initialize(Collection);
|
|||
|
|
FDTFluxCoreModule& DTFluxCore = FModuleManager::Get().LoadModuleChecked<FDTFluxCoreModule>("DTFluxCore");
|
|||
|
|
FString StatusString = UEnum::GetValueAsString(WsStatus);
|
|||
|
|
UE_LOG(logDTFluxNetwork, Log, TEXT("Status is %s"), *StatusString);
|
|||
|
|
UDTFluxNetworkSettings* NetworkSettings = GetMutableDefault<UDTFluxNetworkSettings>();
|
|||
|
|
UDTFluxNetworkSettings::GetWebSocketSettings(NetworkSettings, WsSettings);
|
|||
|
|
UDTFluxNetworkSettings::GetHTTPSettings(NetworkSettings, HttpSettings);
|
|||
|
|
WsClient = MakeShareable<FDTFluxWebSocketClient>(new FDTFluxWebSocketClient());
|
|||
|
|
HttpClient = MakeShareable<FDTFluxHttpClient>(new FDTFluxHttpClient());
|
|||
|
|
RegisterWebSocketEvents();
|
|||
|
|
RegisterHttpEvents();
|
|||
|
|
#if WITH_EDITOR
|
|||
|
|
NetworkSettings->OnDTFluxWebSocketSettingsChanged.AddUFunction(this, FName("WsSettingsChanged"));
|
|||
|
|
NetworkSettings->OnDTFluxHttpSettingsChanged.AddUFunction(this, FName("HttpSettingsChanged"));
|
|||
|
|
#endif
|
|||
|
|
if(WsSettings.bShouldConnectAtStartup)
|
|||
|
|
{
|
|||
|
|
WsClient->SetAddress(ConstructWsAddress(WsSettings.Address, WsSettings.Path, WsSettings.Port));
|
|||
|
|
WsClient->Connect();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::Deinitialize()
|
|||
|
|
{
|
|||
|
|
Super::Deinitialize();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::WsSettingsChanged(const FDTFluxWsSettings& NewWsSettings)
|
|||
|
|
{
|
|||
|
|
// TODO Implement a ClientSelector To retrieve impacted WsClients and populate changes or maybe create a delegate
|
|||
|
|
bool bNeedsReload = WsSettings != NewWsSettings;
|
|||
|
|
|
|||
|
|
WsSettings = NewWsSettings;
|
|||
|
|
// UE_LOG(logDTFluxNetwork, Warning, TEXT("WSocket Settings Changed \n\t Address : %s Path : %s Port : %i\n\tbShouldConnectAtStatup : %s, bShouldAutoReconnectOnClosed %s, bShouldAutoReconnectOnError %s"),
|
|||
|
|
// *NewWsSettings.Address, *NewWsSettings.Path, NewWsSettings.Port,
|
|||
|
|
// NewWsSettings.bShouldConnectAtStartup ? TEXT("True") : TEXT("False"),
|
|||
|
|
// NewWsSettings.bShouldAutoReconnectOnClosed ? TEXT("True") : TEXT("False"),
|
|||
|
|
// NewWsSettings.bShouldAutoReconnectOnError ? TEXT("True") : TEXT("False") );
|
|||
|
|
if( bNeedsReload || WsSettings.bShouldConnectAtStartup)
|
|||
|
|
{
|
|||
|
|
UE_LOG(logDTFluxNetwork, Warning, TEXT("WSocket Settings needs Reloding client"))
|
|||
|
|
ReconnectWs(FName("Ws_Client_0"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::HttpSettingsChanged(const FDTFluxHttpSettings& NewHttpSettings)
|
|||
|
|
{
|
|||
|
|
// TODO Implement a ClientSelector To retrieve impacted HttpClients and populate changes or maybe create a delegate
|
|||
|
|
HttpSettings = NewHttpSettings;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::ReconnectWs(const FName WsClientId)
|
|||
|
|
{
|
|||
|
|
FString NewAddress = ConstructWsAddress(WsSettings.Address, WsSettings.Path, WsSettings.Port);
|
|||
|
|
WsClient->SetAddress(NewAddress);
|
|||
|
|
WsClient->Reconnect();
|
|||
|
|
}
|
|||
|
|
void UFDTFluxNetworkSubsystem::ReconnectHttp(const FName WsClientId)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::RegisterWebSocketEvents()
|
|||
|
|
{
|
|||
|
|
OnWsConnectedEventDelegateHandle =
|
|||
|
|
WsClient->RegisterConnectedEvent().AddUObject(this, &UFDTFluxNetworkSubsystem::OnWebSocketConnected_Subsystem);
|
|||
|
|
OnWsConnectionErrorEventDelegateHandle =
|
|||
|
|
WsClient->RegisterConnectionError()
|
|||
|
|
.AddUObject(this, &UFDTFluxNetworkSubsystem::OnWebSocketConnectionError_Subsystem);
|
|||
|
|
OnWsClosedEventDelegateHandle =
|
|||
|
|
WsClient->RegisterClosedEvent()
|
|||
|
|
.AddUObject(this, &UFDTFluxNetworkSubsystem::OnWebSocketClosedEvent_Subsystem);
|
|||
|
|
OnWsMessageEventDelegateHandle =
|
|||
|
|
WsClient->RegisterMessageEvent()
|
|||
|
|
.AddUObject(this, &UFDTFluxNetworkSubsystem::OnWebSocketMessageEvent_Subsystem);
|
|||
|
|
OnWsMessageSentEventDelegateHandle =
|
|||
|
|
WsClient->RegisterMessageSentEvent()
|
|||
|
|
.AddUObject(this, &UFDTFluxNetworkSubsystem::OnWebSocketMessageSentEvent_Subsystem);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::RegisterHttpEvents()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::UnregisterWebSocketEvents()
|
|||
|
|
{
|
|||
|
|
if(OnWsConnectedEventDelegateHandle.IsValid())
|
|||
|
|
{
|
|||
|
|
WsClient->UnregisterConnectedEvent().Remove(OnWsConnectedEventDelegateHandle);
|
|||
|
|
}
|
|||
|
|
if(OnWsConnectionErrorEventDelegateHandle.IsValid())
|
|||
|
|
{
|
|||
|
|
WsClient->UnregisterConnectionError();
|
|||
|
|
}
|
|||
|
|
if(OnWsClosedEventDelegateHandle.IsValid())
|
|||
|
|
{
|
|||
|
|
WsClient->UnregisterClosedEvent();
|
|||
|
|
}
|
|||
|
|
if(OnWsMessageEventDelegateHandle.IsValid())
|
|||
|
|
{
|
|||
|
|
WsClient->UnregisterMessageEvent();
|
|||
|
|
}
|
|||
|
|
if(OnWsMessageSentEventDelegateHandle.IsValid())
|
|||
|
|
{
|
|||
|
|
WsClient->UnregisterRawMessageEvent();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::UnregisterHttpEvents()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::OnWebSocketConnected_Subsystem()
|
|||
|
|
{
|
|||
|
|
OnWebSocketConnected.Broadcast();
|
|||
|
|
UE_LOG(logDTFluxNetwork, Warning, TEXT("Ws Is Connected with %s"), *WsClient->GetAddress())
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::OnWebSocketConnectionError_Subsystem(const FString& Error)
|
|||
|
|
{
|
|||
|
|
UE_LOG(logDTFluxNetwork, Warning, TEXT("Ws Error with %s : %s"), *WsClient->GetAddress(), *Error);
|
|||
|
|
if(WsSettings.bShouldAutoReconnectOnError)
|
|||
|
|
{
|
|||
|
|
WsClient->Reconnect();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::OnWebSocketClosedEvent_Subsystem(int32 StatusCode, const FString& Reason, bool bWasClean)
|
|||
|
|
{
|
|||
|
|
UE_LOG(logDTFluxNetwork, Warning, TEXT("Ws Error with %s :\n Reason : %s \tStatusCode : %i, bWasClean : %s"),
|
|||
|
|
*WsClient->GetAddress(), *Reason, StatusCode, bWasClean ? TEXT("True") : TEXT("False"));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::OnWebSocketMessageEvent_Subsystem(const FString& MessageString)
|
|||
|
|
{
|
|||
|
|
UE_LOG(logDTFluxNetwork, Warning, TEXT("Ws %s :\nMessage Received : %s"), *WsClient->GetAddress(), *MessageString);
|
|||
|
|
//Do Something With the message
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void UFDTFluxNetworkSubsystem::OnWebSocketMessageSentEvent_Subsystem(const FString& MessageSent)
|
|||
|
|
{
|
|||
|
|
UE_LOG(logDTFluxNetwork, Warning, TEXT("Ws %s :\nMessage Sent: %s"), *WsClient->GetAddress(), *MessageSent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
FString UFDTFluxNetworkSubsystem::ConstructWsAddress(const FString& Address, const FString& Path, const int& Port)
|
|||
|
|
{
|
|||
|
|
FString NewAddress;
|
|||
|
|
if( !Address.Contains("ws://") && !Address.Contains("wss://"))
|
|||
|
|
{
|
|||
|
|
NewAddress += FString("ws://");
|
|||
|
|
}
|
|||
|
|
NewAddress +=Address + FString(":") + FString::FromInt(Port) + Path;
|
|||
|
|
return NewAddress;
|
|||
|
|
// UE_LOG(logDTFluxNetwork, Log, TEXT("NewAddress : %s"), *NewAddress);
|
|||
|
|
}
|