Files
EventCenterPlugin/Source/EventCenterPlugin/Private/EventSourceActor.cpp
T

199 lines
5.6 KiB
C++

#include "EventSourceActor.h"
#include "UObject/Object.h"
DEFINE_LOG_CATEGORY(EventSourceLog);
AEventSourceActor::AEventSourceActor() :
redis(nullptr),
WebSocket(nullptr)
{
UE_LOG(EventSourceLog, Log, TEXT("CTOR()"));
// Set this actor to call Tick() every frame.
// You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//redis = NewObject<URedisObject>(this, TEXT("RedisObject"));
//redis = CreateDefaultSubobject<URedisObject>(TEXT("RedisObject"));
//RootComponent = redis;
}
AEventSourceActor::~AEventSourceActor()
{
// TODO: Memory leak (somehow placing a BP derivation into the editor causes the below lines to segfault)
//delete redis;
//redis = nullptr;
/*if (redis != nullptr) {
delete redis;
redis = nullptr;
}*/
}
void AEventSourceActor::BeginPlay()
{
Super::BeginPlay();
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "BeginPlay() - 2");
UE_LOG(EventSourceLog, Log, TEXT("BeginPlay()"));
if (redis == nullptr) {
UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Creating redis instance"));
redis = NewObject<URedisObject>(this, TEXT("RedisObject"));
}
UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis is null? ? %d"), redis == nullptr);
//UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis host: %s"), redisHost);
UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis port: %d"), redisPort);
//UE_LOG(EventSourceLog, Log, TEXT("BeginPlay(): Redis password (no secrets): %s"), redisPassword);
}
void AEventSourceActor::BeginDestroy()
{
Super::BeginDestroy();
}
void AEventSourceActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
float time = GetGameTimeSinceCreation();
if (time - timerLastTime > 15.0) {
timerLastTime = time;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "seconds elapsed");
redis->Unsubscribe(TEXT("second"));
redis->Subscribe(TEXT("second"));
}
// NB: Handled by RedisObject internal delegate
//redis->Tick(DeltaTime);
}
void AEventSourceActor::ConstructClient()
{
if (redis != nullptr) {
UE_LOG(EventSourceLog, Log, TEXT("Construct(): Redis is NOT null"));
return;
}
UE_LOG(EventSourceLog, Log, TEXT("construct(): redis = new Redis()"));
redis = NewObject<URedisObject>(this, TEXT("RedisObject"));
}
void AEventSourceActor::Init()
{
if (redis == nullptr) {
UE_LOG(EventSourceLog, Log, TEXT("Init(): Redis is null"));
return;
}
//UE_LOG(EventSourceLog, Log, TEXT("construct(): redis = new Redis()"));
//redis = NewObject<URedisObject>(this, TEXT("RedisObject"));
UE_LOG(EventSourceLog, Log, TEXT("Init(): redis->Init()"));
redis->Init(redisHost, redisPort, redisPassword);
}
void AEventSourceActor::Subscribe(const FString& Channel)
{
if (redis == nullptr) {
UE_LOG(EventSourceLog, Log, TEXT("Subscribe(): Redis is null"));
return;
}
UE_LOG(EventSourceLog, Log, TEXT("Subscribe(): redis->Subscribe()"));
redis->Subscribe(Channel);
if (!redis->SubscribeReply.IsBound()) {
UE_LOG(EventSourceLog, Log, TEXT("Subscribe(): binding event"));
redis->SubscribeReply.AddDynamic(this, &AEventSourceActor::InternalSubscribeCallback);
}
}
void AEventSourceActor::Unsubscribe(const FString& Channel)
{
if (redis == nullptr) {
UE_LOG(EventSourceLog, Log, TEXT("Unsubscribe(): Redis is null"));
return;
}
UE_LOG(EventSourceLog, Log, TEXT("Unsubscribe(): redis->Unsubscribe()"));
redis->Unsubscribe(Channel);
}
void AEventSourceActor::InternalSubscribeCallback(FString Channel, FString Message)
{
//UE_LOG(EventSourceLog, Log, TEXT("SubscribeCallback(): %s %s"), Channel, Message);
UE_LOG(EventSourceLog, Log, TEXT("SubscribeCallback(): called"));
if (MyDelegate.IsBound()) {
MyDelegate.Broadcast(Channel, Message);
}
}
// ======================================= web sockets =============================================
void AEventSourceActor::OnConnected()
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "OnConnected");
UE_LOG(EventSourceLog, Log, TEXT("OnConnected"));
WebSocket->SendMessage(TEXT("Hello Server!"));
}
void AEventSourceActor::OnConnectionError(const FString& Error)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "OnConnectionError");
UE_LOG(EventSourceLog, Log, TEXT("OnConnectionError"));
// Connection failed.
UE_LOG(EventSourceLog, Error, TEXT("Failed to connect: %s."), *Error);
}
void AEventSourceActor::OnClosed(int64 StatusCode, const FString& Reason, bool bWasClean)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "OnClosed");
UE_LOG(EventSourceLog, Log, TEXT("OnClosed"));
UE_LOG(EventSourceLog, Warning, TEXT("Connection closed: %d:%s. Clean: %d"), StatusCode, *Reason, bWasClean);
}
void AEventSourceActor::OnMessage(const FString& Message)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "OnMessage");
UE_LOG(EventSourceLog, Log, TEXT("OnMessage"));
UE_LOG(EventSourceLog, Log, TEXT("New message: %s"), *Message);
}
void AEventSourceActor::OnRawMessage(const TArray<uint8>& Data, int32 BytesRemaining)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "OnRawMessage");
UE_LOG(EventSourceLog, Log, TEXT("OnRawMessage"));
UE_LOG(EventSourceLog, Log, TEXT("New binary message: %d bytes and %d bytes remaining."), Data.Num(), BytesRemaining);
}
void AEventSourceActor::OnMessageSent(const FString& Message)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "OnMessageSent");
UE_LOG(EventSourceLog, Log, TEXT("OnMessageSent"));
UE_LOG(EventSourceLog, Log, TEXT("We just sent %s to the server."), *Message);
}
void AEventSourceActor::SetupWebSockets()
{
if (WebSocket == nullptr) {
WebSocket = UBlueprintWebSocket::CreateWebSocket();
}
WebSocket->Connect(TEXT("ws://localhost:8765/"), TEXT("ws"));
}