Skip to content

Civet web server integration plugin for Unreal Engine 4

License

Notifications You must be signed in to change notification settings

PushkinStudio/PsWebServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c3ed37e · Jan 20, 2023

History

48 Commits
Feb 5, 2019
Jan 20, 2023
Feb 5, 2019
Feb 5, 2019
Feb 5, 2019
Mar 8, 2022

Repository files navigation

PsWebServer

Civet web server integration plugin for Unreal Engine 4

HowTo

Web server usage (f.e. in AMyGameMode::BeginPlay()))

// Launch web server
WebServer = NewObject<UPsWebServerWrapper>(this);
WebServer->StartServer();

// Create and register api handler
auto PingHandler = NewObject<UMyServerPingHandler>(this);
PingHandler->SetHeader(TEXT("Server"), TEXT("MyServer/") + MyGI->GetGameVersion());     // Optional header set
WebServer->AddHandler(PingHandler, TEXT("/api/ping"));

If uses UMyServerPingHandler class that defined as:

#pragma once

#include "PsWebServerHandler.h"

#include "MyServerPingHandler.generated.h"

UCLASS()
class UMyServerPingHandler : public UPsWebServerHandler
{
	GENERATED_BODY()

public:
	/** Override to implement your custom logic of request processing */
	virtual void ProcessRequest_Implementation(const FGuid& RequestUniqueId, const FString& RequestData) override;
};

And its definition:

#include "MyServerPingHandler.h"

#include "PsWebServerDefines.h"
#include "VaRestJsonObject.h"

/*
 * Check that request has valid json encoded body and return its copy in response
 */
void UMyServerPingHandler::ProcessRequest_Implementation(const FGuid& RequestUniqueId, const FString& RequestData)
{
	// Validate json format with VaRest
	UVaRestJsonObject* JsonTemp = UVaRestJsonObject::ConstructJsonObject(this);
	if (JsonTemp->DecodeJson(RequestData))
	{
		ProcessRequestFinish(RequestUniqueId, FString::Printf(TEXT("{\"request_data\":%s}"), *RequestData));
		return;
	}

	UE_LOG(LogMyGame, Error, TEXT("%s: can't validate data as json one: %s"), *PS_FUNC_LINE, *RequestData);

	const FString ErrorStr = TEXT(R"({"error":"1000","message":"Request data is not a valid json object"})");
	ProcessRequestFinish(RequestUniqueId, ErrorStr);
}