Skip to content

Commit

Permalink
Initial work on websocket messages
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoaws committed Jul 23, 2024
1 parent e6b479c commit 9cca07e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ WebSocketClient::WebSocketClient(const FString& authToken, const FString& endpoi
const FString ServerProtocol = TEXT("wss");

// Create the WebSocket
TSharedPtr<IWebSocket> Socket = FWebSocketsModule::Get().CreateWebSocket(ServerURL, ServerProtocol);
this->Socket = FWebSocketsModule::Get().CreateWebSocket(ServerURL, ServerProtocol);

// Bind our message handler
Socket->OnMessage().AddRaw(this, &WebSocketClient::OnMessageReceived);
Expand Down Expand Up @@ -53,3 +53,9 @@ void WebSocketClient::OnMessageReceived(const FString & Message) {
// Call the callback function with the received message
this->callback.ExecuteIfBound(Message);
}

void WebSocketClient::SendMessage(const FString& message) {

// Send the message through the WebSocket
this->Socket->Send(message);
}
3 changes: 3 additions & 0 deletions UnrealSample/Source/UnrealSample/AWSGameSDK/WebSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
#pragma once

#include "CoreMinimal.h"
#include "IWebSocket.h"

class WebSocketClient {
public:
DECLARE_DELEGATE_OneParam(FOnMessageReceived, const FString&);
// Constructor that receives an auth_token and a connection endpoint
WebSocketClient(const FString& authToken, const FString& endpoint, FOnMessageReceived callback);
void SendMessage(const FString& message);

private:

TSharedPtr<IWebSocket> Socket;
FOnMessageReceived callback;

void OnMessageReceived(const FString& message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ void USimpleWebsocketChat::OnLoginResultCallback(const UserInfo& userInfo){
// Create a new WebSocket and bind callback
WebSocketClient::FOnMessageReceived messageCallback;
messageCallback.BindUObject(this, &USimpleWebsocketChat::OnMessageReceived);
auto webSocketClient = new WebSocketClient(userInfo.auth_token, this->m_websocketEndpointUrl, messageCallback);
this->m_webSocketClient = new WebSocketClient(userInfo.auth_token, this->m_websocketEndpointUrl, messageCallback);

// Test the Websocket client
this->SetUserName("John Doe");

}

void USimpleWebsocketChat::OnMessageReceived(const FString& message){
Expand All @@ -110,4 +114,21 @@ void USimpleWebsocketChat::OnMessageReceived(const FString& message){
UE_LOG(LogTemp, Display, TEXT("Received message: %s \n"), *message);
if(GEngine)
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Black, FString::Printf(TEXT("Received message: \n %s \n"), *message), false, FVector2D(1.5f, 1.5f));
}

// Websocket messages

void USimpleWebsocketChat::SetUserName(const FString& username){

// Create a JSON object for the set-name message
TSharedPtr<FJsonObject> JsonMessage = MakeShareable(new FJsonObject());
JsonMessage->SetStringField("type", "set-name");
TSharedPtr<FJsonObject> JsonPayload = MakeShareable(new FJsonObject());
JsonPayload->SetStringField("username", username);
JsonMessage->SetObjectField("payload", JsonPayload);
FString JsonString;
TSharedRef<TJsonWriter<>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
FJsonSerializer::Serialize(JsonMessage.ToSharedRef(), JsonWriter);

this->m_webSocketClient->SendMessage(JsonString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "../../AWSGameSDK/AWSGameSDK.h"
#include "../../AWSGameSDK/WebSocketClient.h"
#include "SimpleWebsocketChat.generated.h"


Expand Down Expand Up @@ -35,6 +36,9 @@ class UNREALSAMPLE_API USimpleWebsocketChat : public UActorComponent
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

void SetUserName(const FString& userName);

WebSocketClient *m_webSocketClient;

private:

Expand Down

0 comments on commit 9cca07e

Please sign in to comment.