UE5 使用插件创建C++的HttpServer
参考资料:Aleksey Usanov / HttpServerExample · GitLab【极客日常】在UE4插件中编写一个HTTP Web Server_ue http server web-CSDN博客下面这两篇最重要萌新第一稿~手把手教你优雅地使用HTTP请求调用UE4(UE5)函数(中英双字幕)_哔哩哔哩_bilibili手把手教你如何优雅地用http请求控制UE4(UE5) - 知
参考资料:
Aleksey Usanov / HttpServerExample · GitLab
【极客日常】在UE4插件中编写一个HTTP Web Server_ue http server web-CSDN博客
下面这两篇最重要
萌新第一稿~手把手教你优雅地使用HTTP请求调用UE4(UE5)函数(中英双字幕)_哔哩哔哩_bilibili
手把手教你如何优雅地用http请求控制UE4(UE5) - 知乎
新建一个Acot C++类在你的插件目录下
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Runtime/Online/HTTPServer/Public/HttpServerModule.h" #include "Runtime/Online/HTTPServer/Public/HttpPath.h" #include "HTTPServer/Public/IHttpRouter.h" #include "HTTPServer/Public/HttpServerModule.h" #include "GameFramework/Actor.h" #include "HttpServer.generated.h" DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnreceivedRequestDelegeta,FString,Body); UCLASS() class MYPYTHONBLUEPRINTLIBRARY_API AHttpServer : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AHttpServer(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; FHttpServerModule* HttpServerInstance; TSharedPtr<IHttpRouter> Router; FHttpRouteHandle HttpRouteHandle; FString str; public: UFUNCTION(BlueprintCallable,Category="HttpServer") void InitialzeHttpServer(); UFUNCTION(BlueprintCallable,Category="HttpServer") void UnInitialzeHttpServer(); virtual void BeginDestroy() override; UPROPERTY(BlueprintAssignable,Category="HttpServer") FOnreceivedRequestDelegeta OnreceivedRequestDelegeta; };
// Fill out your copyright notice in the Description page of Project Settings. #include "HttpServer.h" #include "HTTPServer/Public/HttpPath.h" #include "HttpServerModule.h" #include "IHttpRouter.h" // Sets default values AHttpServer::AHttpServer() { // 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 AHttpServer::BeginPlay() { Super::BeginPlay(); } // Called every frame void AHttpServer::Tick(float DeltaTime) { Super::Tick(DeltaTime); } void AHttpServer::InitialzeHttpServer() { HttpServerInstance = &FHttpServerModule::Get(); Router = HttpServerInstance->GetHttpRouter(8888); FHttpPath HttpPath("/msg"); UE_LOG(LogTemp, Error, TEXT("InitialzeHttpServer Start")); HttpRouteHandle = Router->BindRoute(HttpPath,EHttpServerRequestVerbs::VERB_GET|EHttpServerRequestVerbs::VERB_POST,[this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) { for(auto QueryParam : Request.QueryParams ) { UE_LOG(LogTemp, Log, TEXT("QueryParam: %s : %s"), *QueryParam.Key, *QueryParam.Value); } for (auto PathParam : Request.PathParams) { UE_LOG(LogTemp, Log, TEXT("PathParam: %s : %s"), *PathParam.Key, *PathParam.Value); } FString RequestAsFString = UTF8_TO_TCHAR(reinterpret_cast<const char*>(Request.Body.GetData())); UE_LOG(LogTemp, Log, TEXT("Body: %s"), *RequestAsFString); OnComplete(FHttpServerResponse::Ok()); if(OnreceivedRequestDelegeta.IsBound()) { OnreceivedRequestDelegeta.Broadcast(RequestAsFString); } UE_LOG(LogTemp, Error, TEXT("true!")); return true; }); HttpServerInstance->StartAllListeners(); UE_LOG(LogTemp, Log, TEXT("HttpServerStartAllListeners!")); } void AHttpServer::UnInitialzeHttpServer() { if(HttpServerInstance) { HttpServerInstance->StopAllListeners(); } if(Router) { Router->UnbindRoute(HttpRouteHandle); } //UE_LOG(LogTemp, Log, TEXT("HttpServerUninitialized!")); } void AHttpServer::BeginDestroy() { Super::BeginDestroy(); UnInitialzeHttpServer(); }
把这个httpserver放到关卡蓝图内
在关卡蓝图内先初始化再调用事件
或者使用事件传参
.h
UFUNCTION(BlueprintImplementableEvent,Category="HttpServer") void WechatMsg(const FString& msg );
.cpp
void AHttpServer::InitialzeHttpServer() { HttpServerInstance = &FHttpServerModule::Get(); Router = HttpServerInstance->GetHttpRouter(8888); FHttpPath HttpPath("/msg"); UE_LOG(LogTemp, Error, TEXT("InitialzeHttpServer Start")); HttpRouteHandle = Router->BindRoute(HttpPath,EHttpServerRequestVerbs::VERB_GET|EHttpServerRequestVerbs::VERB_POST,[this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) { for(auto QueryParam : Request.QueryParams ) { UE_LOG(LogTemp, Log, TEXT("QueryParam: %s : %s"), *QueryParam.Key, *QueryParam.Value); } for (auto PathParam : Request.PathParams) { UE_LOG(LogTemp, Log, TEXT("PathParam: %s : %s"), *PathParam.Key, *PathParam.Value); } FString RequestAsFString = UTF8_TO_TCHAR(reinterpret_cast<const char*>(Request.Body.GetData())); UE_LOG(LogTemp, Log, TEXT("Body: %s"), *RequestAsFString); OnComplete(FHttpServerResponse::Ok()); if(OnreceivedRequestDelegeta.IsBound()) { OnreceivedRequestDelegeta.Broadcast(RequestAsFString); WechatMsg(RequestAsFString); //这里传参 } UE_LOG(LogTemp, Error, TEXT("true!")); return true; }); HttpServerInstance->StartAllListeners(); UE_LOG(LogTemp, Log, TEXT("HttpServerStartAllListeners!")); }
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)