A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
ApexWebSocket.cpp
Go to the documentation of this file.
1// Copyright 2023 PixoVR Corp. All Rights Reserved.
2
3#include "ApexWebSocket.h"
4#include "WebSocketsModule.h"
5#include "IWebSocket.h"
6#include "ApexLibrary.h"
7
8DEFINE_LOG_CATEGORY_STATIC(LogApexWebSocket, Log, All);
9
10#define LogAPEX(pmt, ...) UE_LOG(LogApexWebSocket, Log, TEXT(pmt), ##__VA_ARGS__)
11#define WarnAPEX(pmt, ...) UE_LOG(LogApexWebSocket, Warning, TEXT(pmt), ##__VA_ARGS__)
12#define ErrorAPEX(pmt, ...) UE_LOG(LogApexWebSocket, Error, TEXT(pmt), ##__VA_ARGS__)
13#define FatalAPEX(pmt, ...) UE_LOG(LogApexWebSocket, Fatal, TEXT(pmt), ##__VA_ARGS__)
14
19
21{
22 if (WebSocket.IsValid() && WebSocket->IsConnected())
23 {
24 // Clear the OnClosed event to prevent some access errors when the app is shutting down.
25 WebSocket->OnClosed().Clear();
26 WebSocket->Close();
27 }
28}
29
30bool ApexWebSocket::AttemptConnect(FString Uri, FString Protocol)
31{
32 if (WebSocket.IsValid() && WebSocket->IsConnected())
33 return false;
34
36
37 if (!WebSocket.IsValid())
38 {
39 SetupSocket(WebSocket, Uri, Protocol);
40 }
41
42 WebSocket->Connect();
43
44 return true;
45}
46
48{
49 if (WebSocket.IsValid() && WebSocket->IsConnected())
50 return true;
51
52 return false;
53}
54
64
65void ApexWebSocket::SetupSocket(TSharedPtr<IWebSocket>& Socket, FString Uri, FString Protocol)
66{
67 Socket = FWebSocketsModule::Get().CreateWebSocket(Uri, Protocol);
68
69 Socket->OnConnected().AddLambda([&]() -> void {
70 LogAPEX("Apex websocket connected.");
72 ApexConnectedEvent.Broadcast();
73 });
74
75 Socket->OnConnectionError().AddLambda([&](const FString& Error) -> void {
76 ErrorAPEX("Failed to connect with error: %s", *Error);
79 });
80
81 Socket->OnClosed().AddLambda([&](int32 StatusCode, const FString& Reason, bool bWasClean) -> void {
82 LogAPEX("Socket closed with status %d, for reason %s", StatusCode, *Reason);
84 ApexDisconnectedEvent.Broadcast(StatusCode, Reason, bWasClean);
85 });
86
87 Socket->OnMessage().AddLambda([&](const FString& Message) -> void {
88 // This code will run when we receive a string message from the server.
89 LogAPEX("Message received: %s", *Message);
91 if (!RestSubsystem)
92 {
93 ErrorAPEX("Unable to parse message.");
94 return;
95 }
96
97 UVaRestJsonObject* MessageJsonObject = RestSubsystem->DecodeJsonObject(Message);
98 if (MessageJsonObject->HasField("auth_code"))
99 {
100 HandleAuthCodeReceived(MessageJsonObject);
101 }
102
103 if (MessageJsonObject->HasField("token"))
104 {
105 HandleAuthenticateComplete(MessageJsonObject);
106 }
107 });
108
109 Socket->OnRawMessage().AddLambda([&](const void* Data, SIZE_T Size, SIZE_T BytesRemaining) -> void {
110 // This code will run when we receive a raw (binary) message from the server.
111 LogAPEX("A raw message was received.");
112 });
113
114 Socket->OnMessageSent().AddLambda([&](const FString& MessageString) -> void {
115 LogAPEX("Apex message sent.");
117 });
118}
119
121{
122 if (PendingWebsocketRequests.Num() > 0)
123 {
124 auto NextRequestType = PendingWebsocketRequests.Pop();
125
126 switch (NextRequestType)
127 {
129 {
130 SendData("{\"action\": \"authcode\" }");
131 }
132 break;
133 default:
134 ErrorAPEX("Invalid request type submitted of type %d", NextRequestType);
135 }
136 }
137}
138
140{
141 FAPEXAuthenticationCode APEXAuthCode;
142 APEXAuthCode.FromJsonObject(JsonObject->GetRootObject());
143 ApexAuthCodeRetrievedEvent.Broadcast(APEXAuthCode.AuthCode);
144}
145
150
155
156void ApexWebSocket::SendData(FString Data)
157{
158 if (!WebSocket->IsConnected())
159 {
160 return;
161 }
162
163 WebSocket->Send(Data);
164}
165
166#undef FatalAPEX
167#undef ErrorAPEX
168#undef WarnAPEX
169#undef LogAPEX
#define LogAPEX(pmt,...)
Definition ApexAPI.cpp:13
#define ErrorAPEX(pmt,...)
Definition ApexAPI.cpp:15
DEFINE_LOG_CATEGORY_STATIC(LogApexWebSocket, Log, All)
@ AuthorizationCode
bool IsWebsocketConnected()
void SetupSocket(TSharedPtr< IWebSocket > &Socket, FString Uri, FString Protocol)
void SendData(FString Data)
void RequestAuthorizationCode()
FApexSocketConnectFailed ApexConnectFailedEvent
void HandleAuthenticateComplete(UVaRestJsonObject *JsonObject)
bool AttemptConnect(FString Uri, FString Protocol="wss")
void HandleAuthCodeReceived(UVaRestJsonObject *JsonObject)
FApexSocketAuthCodeRetrieved ApexAuthCodeRetrievedEvent
TSharedPtr< IWebSocket > WebSocket
FApexSocketConnected ApexConnectedEvent
FApexSocketDisconnected ApexDisconnectedEvent
FApexSocketAuthenticateComplete ApexAuthenticateCompleteEvent
TArray< ApexWebSocketRequestType > PendingWebsocketRequests
void ProcessNextRequest()
static UVaRestSubsystem * GetRestSubsystem()
UCLASS(BlueprintType, Blueprintable)
TSharedRef< FJsonObject > & GetRootObject()
bool HasField(const FString &FieldName) const
UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
UVaRestJsonObject * DecodeJsonObject(const FString &JsonString)
UFUNCTION(BlueprintCallable, Category = "VaRest|Subsystem")
USTRUCT(BlueprintType)
virtual void FromJsonObject(const TSharedPtr< FJsonObject > &JObject) override
FString AuthCode
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Apex|Authentication Code")