UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LwsWebSocket.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Containers/Ticker.h"
7#include "Containers/Queue.h"
9
10#if WITH_WEBSOCKETS && WITH_LIBWEBSOCKETS
11
12#include "IWebSocket.h"
13
14#if PLATFORM_WINDOWS
16#endif
17
19#include "libwebsockets.h"
21
22#if PLATFORM_WINDOWS
24#endif
25
27struct FLwsSendBuffer
28{
35 FLwsSendBuffer(const uint8* Data, const SIZE_T Size, const bool bInIsBinary);
36
42 int32 GetPayloadSize() const;
43
48 bool IsDone() const { return !HasError() && BytesWritten >= GetPayloadSize(); }
49
54 bool HasError() const { return bHasError; }
55
57 const bool bIsBinary;
59 int32 BytesWritten;
61 TArray<uint8> Payload;
63 bool bHasError;
64};
65
68{
76
78 TArray<uint8> Payload;
81};
82
84
87{
95
97 TArray<uint8> Payload;
99 const bool bIsLastFragment;
100};
101
103
106{
111 FLwsReceiveBufferText(FString&& InText);
112
114 const FString Text;
115};
116
118
120
121class FLwsWebSocket
122 : public IWebSocket
123 , public TSharedFromThis<FLwsWebSocket>
124{
125
126public:
128 virtual ~FLwsWebSocket();
129
130 // IWebSocket
131 virtual void Connect() override;
132 virtual void Close(const int32 Code = 1000, const FString& Reason = FString()) override;
133
134 virtual bool IsConnected() override
135 {
136 return LastGameThreadState == EState::Connected || LastGameThreadState == EState::ClosingByRequest;
137 }
138
139 virtual void Send(const FString& Data);
140 virtual void Send(const void* Data, SIZE_T Size, bool bIsBinary) override;
142
144 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketConnectedEvent, FWebSocketConnectedEvent);
145 virtual FWebSocketConnectedEvent& OnConnected() override
146 {
147 return ConnectedEvent;
148 }
149
151 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketConnectionErrorEvent, FWebSocketConnectionErrorEvent);
153 {
155 }
156
158 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketClosedEvent, FWebSocketClosedEvent);
159 virtual FWebSocketClosedEvent& OnClosed() override
160 {
161 return ClosedEvent;
162 }
163
165 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketMessageEvent, FWebSocketMessageEvent);
166 virtual FWebSocketMessageEvent& OnMessage() override
167 {
168 return MessageEvent;
169 }
170
172 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketBinaryMessageEvent, FWebSocketBinaryMessageEvent);
174 {
175 return BinaryMessageEvent;
176 }
177
179 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketRawMessageEvent, FWebSocketRawMessageEvent);
180 virtual FWebSocketRawMessageEvent& OnRawMessage() override
181 {
182 return RawMessageEvent;
183 }
184
185
186 DECLARE_DERIVED_EVENT(FLwsWebSocket, IWebSocket::FWebSocketMessageSentEvent, FWebSocketMessageSentEvent);
187 virtual FWebSocketMessageSentEvent& OnMessageSent() override
188 {
189 return OnMessageSentEvent;
190 }
191
193 int LwsCallback(lws* Instance, lws_callback_reasons Reason, void* Data, size_t Length);
194
198 void GameThreadTick();
199
203 void GameThreadFinalize();
204
211
215 void LwsThreadTick();
216
217private:
218 // Private token only allows members or friends to call MakeShared
219 struct FPrivateToken { explicit FPrivateToken() = default; };
220
221 friend class FLwsWebSocketsManager;
222
223public:
225 FLwsWebSocket(FPrivateToken, const FString& Url, const TArray<FString>& Protocols, const FString& UpgradeHeader, uint64 TextMessageMemoryLimit);
226
227private:
233
235 void SendFromQueue();
236
238 void ClearData();
239
244 bool WriteBuffer(FLwsSendBuffer& Buffer);
245
246private:
247
250
252 enum class EState : uint8
253 {
255 None,
261 Connected,
265 Closed,
267 Error,
268 };
269
271 static const TCHAR* ToString(const EState InState);
272
274 struct FClosedReason
275 {
277 FString Reason;
281 bool bWasClean;
282 };
283
285 EState State;
289 EState LastGameThreadState;
290
292 struct FCloseRequest
293 {
295 FCloseRequest() : Code(0), Reason(nullptr) {}
296
298 int32 Code;
300 ANSICHAR *Reason;
301 };
302
305
308
309 // Events
313 FWebSocketMessageEvent MessageEvent;
317
319 struct lws *LwsConnection;
321 FString Url;
325 FString UpgradeHeader;
326
342
343
345 FString ReceiveBuffer;
354
356
357 // Unique identifier for logging
361 const int32 Identifier;
362};
363
365
366#endif
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_DERIVED_EVENT(OwningType, BaseTypeEvent, EventName)
Definition Delegate.h:231
JsonWriter Close()
#define THIRD_PARTY_INCLUDES_START
Definition GenericPlatformCompilerPreSetup.h:63
::FCriticalSection FTransactionallySafeCriticalSection
Definition TransactionallySafeCriticalSection.h:16
uint32 Size
Definition VulkanMemory.cpp:4034
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
Definition IWebSocket.h:7
virtual FWebSocketRawMessageEvent & OnRawMessage()=0
virtual FWebSocketConnectionErrorEvent & OnConnectionError()=0
virtual FWebSocketClosedEvent & OnClosed()=0
virtual void Connect()=0
virtual FWebSocketBinaryMessageEvent & OnBinaryMessage()=0
virtual FWebSocketConnectedEvent & OnConnected()=0
virtual void SetTextMessageMemoryLimit(uint64 TextMessageMemoryLimit)=0
virtual bool IsConnected()=0
virtual FWebSocketMessageSentEvent & OnMessageSent()=0
virtual FWebSocketMessageEvent & OnMessage()=0
Definition Array.h:670
Definition Queue.h:48
Definition SharedPointer.h:1640
Definition SharedPointer.h:153
Definition UniquePtr.h:107
FString ToString(uint16 Value)
Definition PathFollowingComponent.cpp:82
Definition Voronoi.cpp:10
State
Definition PacketHandler.h:88