Материалы к видео про WebSocket на Unreal Engine 4
Включаю только список необходимых c++ файлов
TicTacToe.Build.cs
using UnrealBuildTool;
public class TicTacToe : ModuleRules
{
public TicTacToe(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "WebSockets" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
public class TicTacToe : ModuleRules
{
public TicTacToe(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "WebSockets" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
WebSocketActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "WebSockets/Public/IWebSocket.h"
#include "WebSocketActor.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FWebSocketMessageDelegate, const FString&, MessageString);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWebSocketConnectedDelegate);
UCLASS(Blueprintable)
class AWebSocketActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AWebSocketActor();
TSharedPtr<IWebSocket> WebSocket;
UPROPERTY(BlueprintAssignable, Category = "WebSocket")
FWebSocketMessageDelegate OnMessageEvent;
UPROPERTY(BlueprintAssignable, Category = "WebSocket")
FWebSocketConnectedDelegate OnConnectedEvent;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "WebSocket")
void CreateWebSocket(FString Address);
UFUNCTION(BlueprintCallable, Category = "WebSocket")
void Send(FString Data);
UFUNCTION(BlueprintCallable, Category = "WebSocket")
void Connect();
UFUNCTION(BlueprintCallable, Category = "WebSocket")
bool IsConnected();
};
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "WebSockets/Public/IWebSocket.h"
#include "WebSocketActor.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FWebSocketMessageDelegate, const FString&, MessageString);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWebSocketConnectedDelegate);
UCLASS(Blueprintable)
class AWebSocketActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AWebSocketActor();
TSharedPtr<IWebSocket> WebSocket;
UPROPERTY(BlueprintAssignable, Category = "WebSocket")
FWebSocketMessageDelegate OnMessageEvent;
UPROPERTY(BlueprintAssignable, Category = "WebSocket")
FWebSocketConnectedDelegate OnConnectedEvent;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "WebSocket")
void CreateWebSocket(FString Address);
UFUNCTION(BlueprintCallable, Category = "WebSocket")
void Send(FString Data);
UFUNCTION(BlueprintCallable, Category = "WebSocket")
void Connect();
UFUNCTION(BlueprintCallable, Category = "WebSocket")
bool IsConnected();
};
WebSocketActor.cpp
#include "WebSocketActor.h"
#include "Modules/ModuleManager.h"
#include "WebSockets/Public/WebSocketsModule.h"
#include "WebSockets/Public/IWebSocket.h"
// Sets default values
AWebSocketActor::AWebSocketActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AWebSocketActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AWebSocketActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AWebSocketActor::CreateWebSocket(FString Address)
{
#if WEBSOCKETS_PACKAGE==1
UE_LOG(LogTemp, Warning, TEXT("WEBSOCKETS_PACKAGE=1")); //prints in all cases
#endif
#if defined(WITH_WEBSOCKETS)
#if WEBSOCKETS_PACKAGE==1
UE_LOG(LogTemp, Warning, TEXT("WEBSOCKETS_PACKAGE=1")); //prints in all cases
#endif
#endif
#if defined(WITH_LIBWEBSOCKETS)
#if WITH_LIBWEBSOCKETS==1
UE_LOG(LogTemp, Warning, TEXT("WITH_LIBWEBSOCKETS=1")); //prints in all cases
#endif
#endif
FModuleManager::Get().LoadModule("WebSockets");
FWebSocketsModule * WsModule = &FWebSocketsModule::Get();
if (WsModule == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("WebSocketsModule is null"));
WebSocket = nullptr;
}
else
{
UE_LOG(LogTemp, Log, TEXT("WebSocketsModule is valid"));
TMap<FString, FString> Headers;
WebSocket = WsModule->CreateWebSocket(Address, TEXT("NULL"), Headers);
WebSocket->OnMessage().AddLambda([&](FString MessageText)
{
OnMessageEvent.Broadcast(MessageText);
});
WebSocket->OnConnected().AddLambda([&]()
{
OnConnectedEvent.Broadcast();
});
}
}
void AWebSocketActor::Send(FString Data)
{
WebSocket->Send(Data);
}
void AWebSocketActor::Connect()
{
WebSocket->Connect();
}
bool AWebSocketActor::IsConnected()
{
return WebSocket->IsConnected();
}
#include "Modules/ModuleManager.h"
#include "WebSockets/Public/WebSocketsModule.h"
#include "WebSockets/Public/IWebSocket.h"
// Sets default values
AWebSocketActor::AWebSocketActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AWebSocketActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AWebSocketActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AWebSocketActor::CreateWebSocket(FString Address)
{
#if WEBSOCKETS_PACKAGE==1
UE_LOG(LogTemp, Warning, TEXT("WEBSOCKETS_PACKAGE=1")); //prints in all cases
#endif
#if defined(WITH_WEBSOCKETS)
#if WEBSOCKETS_PACKAGE==1
UE_LOG(LogTemp, Warning, TEXT("WEBSOCKETS_PACKAGE=1")); //prints in all cases
#endif
#endif
#if defined(WITH_LIBWEBSOCKETS)
#if WITH_LIBWEBSOCKETS==1
UE_LOG(LogTemp, Warning, TEXT("WITH_LIBWEBSOCKETS=1")); //prints in all cases
#endif
#endif
FModuleManager::Get().LoadModule("WebSockets");
FWebSocketsModule * WsModule = &FWebSocketsModule::Get();
if (WsModule == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("WebSocketsModule is null"));
WebSocket = nullptr;
}
else
{
UE_LOG(LogTemp, Log, TEXT("WebSocketsModule is valid"));
TMap<FString, FString> Headers;
WebSocket = WsModule->CreateWebSocket(Address, TEXT("NULL"), Headers);
WebSocket->OnMessage().AddLambda([&](FString MessageText)
{
OnMessageEvent.Broadcast(MessageText);
});
WebSocket->OnConnected().AddLambda([&]()
{
OnConnectedEvent.Broadcast();
});
}
}
void AWebSocketActor::Send(FString Data)
{
WebSocket->Send(Data);
}
void AWebSocketActor::Connect()
{
WebSocket->Connect();
}
bool AWebSocketActor::IsConnected()
{
return WebSocket->IsConnected();
}