Created
May 25, 2017 11:24
-
-
Save sudarshanReddykurri/dd789b03bdb33838a7f5143f1dfc6d48 to your computer and use it in GitHub Desktop.
Get Sever Data into UE4 :P
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#include "BinaryUpload.h" | |
#include <string> | |
#include "FileUploader.h" | |
// Sets default values | |
AFileUploader::AFileUploader() | |
{ | |
// 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 AFileUploader::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
//uploadUrl = "http://localhost/WebApi/testfile.php"; | |
} | |
// Called every frame | |
void AFileUploader::Tick(float DeltaTime) | |
{ | |
Super::Tick(DeltaTime); | |
} | |
bool AFileUploader::UploadFile(const FString & filePath, const FString & uploadUrl) | |
{ | |
TSharedPtr<IHttpRequest> _request; | |
_request = FHttpModule::Get().CreateRequest(); | |
//(&FHttpModule::Get()) | |
_request->SetURL(uploadUrl); | |
_request->SetVerb(TEXT("POST")); | |
_request->SetHeader(TEXT("Content-Type"), "application/octet-stream"); | |
//_request->SetContentAsString("Hello From Server"); | |
/*_request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent"); | |
_request->SetHeader(TEXT("Host"), "http://www.myDomain.com"); | |
_request->SetHeader("Content-Type", TEXT("application/json"));*/ | |
//or "application/x-www-form-urlencoded" | |
TArray<uint8> fileData; | |
FFileHelper::LoadFileToArray(fileData, *filePath); | |
_request->SetContent(fileData); | |
_request->OnProcessRequestComplete().BindUObject(this, &AFileUploader::OnResponseReceived); | |
/*_request->OnRequestProgress().BindRaw(this, &AFileUploader::OnRequestProgress);*/ | |
UE_LOG(LogTemp, Warning, TEXT("File Loaded & is on process request")); | |
return _request->ProcessRequest(); | |
} | |
void AFileUploader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) | |
{ | |
if (bWasSuccessful) | |
{ | |
ResponseStr = Response->GetContentAsString(); | |
} | |
if (!Response.IsValid()) { | |
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Invalid Http Response..!!"))); | |
UE_LOG(LogTemp, Warning, TEXT("Invalid Http Response..!!")); | |
} | |
else if (Response->GetResponseCode() == 200) { | |
FString msg = Response->GetContentAsString(); | |
//HeartRate = FCString::Atoi(*msg); | |
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(" Valid Http Response.. %s"), *msg)); | |
UE_LOG(LogTemp, Warning, TEXT("%s"), *msg); | |
} | |
else { | |
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Error code %d"), Response->GetResponseCode())); | |
UE_LOG(LogTemp, Warning, TEXT("%d"), Response->GetResponseCode()); | |
} | |
} | |
//void AFileUploader::OnRequestProgress(FHttpRequestPtr request, int32 bytesSent, int32 bytesReceived) | |
//{ | |
// | |
// | |
// | |
//} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "GameFramework/Actor.h" | |
#include "RandomStream.h" | |
#include "Networking.h" | |
#include "Http.h" | |
#include "Base64.h" | |
#include "FileUploader.generated.h" | |
UCLASS() | |
class BINARYUPLOAD_API AFileUploader : public AActor | |
{ | |
GENERATED_BODY() | |
public: | |
// Sets default values for this actor's properties | |
AFileUploader(); | |
protected: | |
// Called when the game starts or when spawned | |
virtual void BeginPlay() override; | |
public: | |
// Called every frame | |
virtual void Tick(float DeltaTime) override; | |
/*UPROPERTY(EditAnywhere, BlueprintReadWrite) | |
FString updateUrl;*/ | |
UFUNCTION(BlueprintCallable, Category = "FileUpload") | |
bool UploadFile(const FString& filePath, const FString& uploadUrl); | |
//void OnProcessRequestComplete(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful); | |
void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); | |
/*UFUNCTION(BlueprintCallable, Category = "FileUpload") | |
void OnRequestProgress(FHttpRequestPtr request, int32 bytesSent, int32 bytesReceived);*/ | |
FString ResponseStr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment