VaRest

From ИМЛ Вики
Jump to: navigation, search
  • C++ example
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameMode.h"
#include "VaRestSubsystem.h"

#include "GameplayGM.generated.h"

/**
 *
 */
UCLASS()
class ONLINEGAMEV2_API AGameplayGM : public AGameMode
{
	GENERATED_BODY()

		AGameplayGM(const FObjectInitializer& ObjectInitializer);


public:

	UPROPERTY()
		UVaRestSubsystem* VaRestSubsystem;
	UPROPERTY()
		UVaRestRequestJSON* JsonRequest;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
		TMap<FString, FString> ClientOptionsStrings;

	void PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage) override;

	UFUNCTION()
		void OnRequestComplete(UVaRestRequestJSON* Request);





};
// Fill out your copyright notice in the Description page of Project Settings.


#include "GameplayGM.h"
#include "Kismet/GameplayStatics.h"
#include "Containers/Array.h"
#include "Containers/Map.h"
#include "VaRest.h"
#include "VaRestDefines.h"
#include "VaRestJsonObject.h"
#include "VaRestJsonValue.h"
#include "VaRestLibrary.h"
#include "VaRestRequestJSON.h"
#include "VaRestSettings.h"
#include "VaRestSubsystem.h"
#include "VaRestTypes.h"

AGameplayGM::AGameplayGM(const FObjectInitializer& ObjectInitializer)
{
	
}

void AGameplayGM::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage)
{
	
	VaRestSubsystem = GEngine->GetEngineSubsystem<UVaRestSubsystem>();
	JsonRequest = VaRestSubsystem->ConstructVaRestRequestExt(EVaRestRequestVerb::POST, EVaRestRequestContentType::json);
	
	
	UVaRestJsonObject* JsonObject = JsonRequest->GetRequestObject();
	JsonObject->SetStringField("key", "value");

	JsonRequest->ProcessURL("http://localhost:5000/api/auth/login");

	JsonRequest->OnRequestComplete.AddDynamic(this, &AGameplayGM::OnRequestComplete);

	
	
	//FString login = UGameplayStatics::ParseOption(Options, TEXT("login"));
	//FString password = UGameplayStatics::ParseOption(Options, TEXT("password"));
	
	//UE_LOG(LogTemp, Warning, TEXT("login: %s, pass: %s"), *login, *password);
}

void AGameplayGM::OnRequestComplete(UVaRestRequestJSON* Request)
{
	JsonRequest->OnRequestComplete.RemoveDynamic(this, &AGameplayGM::OnRequestComplete);
	Request->GetResponseContentAsString();
	UE_LOG(LogTemp, Warning, TEXT("Response: %s"), *Request->GetResponseContentAsString());
}