Skip to content

Commit

Permalink
Added propper ping
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Kruggel committed May 13, 2022
1 parent f41fb67 commit 3dc18e2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
Binary file modified Content/ThirdPersonBP/Blueprints/ThirdPersonCharacter.uasset
Binary file not shown.
8 changes: 8 additions & 0 deletions Plugins/HypeRateHeartbeat/Config/FilterPlugin.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[FilterPlugin]
; This section lists additional files which will be packaged along with your plugin. Paths should be listed relative to the root plugin directory, and
; may include "...", "*", and "?" wildcards to match directories, files, and individual characters respectively.
;
; Examples:
; /README.txt
; /Extras/...
; /Binaries/ThirdParty/*.dll
10 changes: 5 additions & 5 deletions Plugins/HypeRateHeartbeat/HypeRateHeartbeat.uplugin
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0",
"FriendlyName": "HypeRateHeartbeat",
"Description": "HypeRate Heartbeat Plugin",
"Category": "Other",
"FriendlyName": "HypeRate.io Heartbeat receiver",
"Description": "HypeRate.io Unreal Engine Plugin",
"Category": "Hype Rate",
"CreatedBy": "Max 'ReDiGermany' Kruggel",
"CreatedByURL": "https://github.com/ReDiGermany",
"DocsURL": "",
"DocsURL": "https://github.com/ReDiGermany/HypeRateUE",
"MarketplaceURL": "",
"SupportURL": "",
"SupportURL": "https://github.com/ReDiGermany/HypeRateUE/issues",
"CanContainContent": true,
"IsBetaVersion": false,
"IsExperimentalVersion": false,
Expand Down
Binary file modified Plugins/HypeRateHeartbeat/Resources/Icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
bool UBPHypeRateHeartbeat::isConnected = false;
int UBPHypeRateHeartbeat::lastHeartBeat = 80;
TSharedPtr<IWebSocket> UBPHypeRateHeartbeat::Socket = nullptr;
std::thread UBPHypeRateHeartbeat::thr = std::thread([]{

while (true) {
if (UBPHypeRateHeartbeat::isConnected) {
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] Ping?"));
if (UBPHypeRateHeartbeat::Socket->IsConnected()) {
UBPHypeRateHeartbeat::Socket->Send("{\"topic\": \"phoenix\",\"event\": \"heartbeat\",\"payload\": {},\"ref\": 0}");
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] Ping"));
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(20 * 1000));
}
});


void UBPHypeRateHeartbeat::Connect(FString Topic, FString WebsocketKey)
Expand All @@ -17,8 +30,7 @@ void UBPHypeRateHeartbeat::Connect(FString Topic, FString WebsocketKey)
const FString ServerProtocol = TEXT("wss"); // The WebServer protocol you want to use.

Socket = FWebSocketsModule::Get().CreateWebSocket(ServerURL, ServerProtocol);
// We bind all available events
Socket->OnConnected().AddLambda([&, Topic]() -> void {
Socket->OnConnected().AddLambda([ Topic]() -> void {
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] OnConnected"));

FString s = FString("{\"topic\": \"hr:" + Topic + "\", \"event\": \"phx_join\",\"payload\": {},\"ref\": 0}");
Expand All @@ -29,7 +41,7 @@ void UBPHypeRateHeartbeat::Connect(FString Topic, FString WebsocketKey)
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] Connection to websocket server has been closed with status code: \"%d\" and reason: \"%s\"."), StatusCode, *Reason);
});

Socket->OnMessage().AddLambda([&](const FString& Message) -> void {
Socket->OnMessage().AddLambda([](const FString& Message) -> void {
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] OnMessage %s"), *Message);
TSharedPtr<FJsonObject> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(*Message);
Expand All @@ -41,7 +53,6 @@ void UBPHypeRateHeartbeat::Connect(FString Topic, FString WebsocketKey)

UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] OnMessage hr %s"), *FString::FromInt(hr));
lastHeartBeat = hr;
//HeartBeatUpdate(hr);
}
});

Expand All @@ -52,13 +63,6 @@ void UBPHypeRateHeartbeat::Connect(FString Topic, FString WebsocketKey)
Socket->OnConnectionError().AddLambda([](const FString& Error) -> void {
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] OnConnectionError %s"), *Error);
});
std::thread([&]() {
while (Socket->IsConnected()) {
Socket->Send("{\"topic\": \"phoenix\",\"event\": \"heartbeat\",\"payload\": {},\"ref\": 0}");
std::this_thread::sleep_for(std::chrono::milliseconds(20 * 1000));
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] Ping"));
}
}).detach();
Socket->Connect();
}

Expand All @@ -73,6 +77,7 @@ void UBPHypeRateHeartbeat::Disconnect() {
if (!Socket->IsConnected()) return;
isConnected = false;
Socket->Close();
//thr.join();
UE_LOG(LogTemp, Log, TEXT("[HYPERATE::DEV] Closed"));
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ class HYPERATEHEARTBEAT_API UBPHypeRateHeartbeat : public UBlueprintFunctionLibr
static bool isConnected;

static TSharedPtr<IWebSocket> Socket;

static std::thread thr;
};

0 comments on commit 3dc18e2

Please sign in to comment.