Files
UnrealEngine/Engine/Plugins/Media/PixelStreaming2/Source/PixelStreaming2Servers/Private/WebSocketProbe.h
Brandyn / Techy fcc1b09210 init
2026-04-04 15:40:51 -05:00

66 lines
1.5 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "IWebSocket.h"
#include "WebSocketsModule.h"
#include "Templates/SharedPointer.h"
#include "Logging.h"
#include "ServerUtils.h"
namespace UE::PixelStreaming2Servers
{
/*
* A utility class that tries to establish a websocket connection.
* Useful for testing whether servers have come online yet.
*/
class FWebSocketProbe
{
private:
TSharedRef<IWebSocket> WebSocket;
FThreadSafeBool bShouldAttemptReconnect;
FThreadSafeBool bCloseRequested;
public:
FWebSocketProbe(FURL Url, TArray<FString> Protocols = TArray<FString>())
: WebSocket(FWebSocketsModule::Get().CreateWebSocket(Utils::ToString(Url), Protocols))
, bShouldAttemptReconnect(true)
, bCloseRequested(false)
{
WebSocket->OnConnectionError().AddLambda([Url, &bShouldAttemptReconnect = bShouldAttemptReconnect](const FString& Error) {
UE_LOG(LogPixelStreaming2Servers, Log, TEXT("Probing websocket %s | Msg= \"%s\" | Retrying..."), *Utils::ToString(Url), *Error);
bShouldAttemptReconnect = true;
});
}
void Close()
{
if (WebSocket->IsConnected() && !bCloseRequested.AtomicSet(true))
{
WebSocket->Close();
}
}
bool IsConnected() const
{
return WebSocket->IsConnected();
}
bool Probe()
{
bool bIsConnected = WebSocket->IsConnected();
if (!bIsConnected && bShouldAttemptReconnect)
{
WebSocket->Connect();
bShouldAttemptReconnect = false;
bCloseRequested = false;
}
return bIsConnected;
}
};
} // namespace UE::PixelStreaming2Servers