x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How to setup a REST server in Unreal?

Hey all! I am confronted with a requirement to setup a REST server in an unreal project in order to send requests from some clients to this server which should modify the projects environment, behaviour, etc..

First shot i took was trying to include cpprestsdk, also known as Casablanca, into the StrategyGame example project. I linked the lib using the Build.cs and included the REST server code into the Module.cpp on Startup like this.

 #include "StrategyGame.h"
 
 #include "StrategyHUDSoundsWidgetStyle.h"
 #include "StrategyHUDWidgetStyle.h"
 #include "StrategyMenuWidgetStyle.h"
 
 //-------------------------------------------------------
 #include <cpprest/http_client.h>
 #include <cpprest/http_listener.h>
 
 using namespace web;
 using namespace web::http;
 using namespace web::http::experimental::listener;
 using namespace utility;
 //-------------------------------------------------------
 
 class FStrategyGameModule : public FDefaultGameModuleImpl
 {
     virtual void StartupModule() override
     {
         //-------------------------------------------------------
         http_listener* m_listener = nullptr;
 
         {
             string_t strScheme = U("http");
             string_t strHost = U("localhost");
             string_t strPort = U("7654");
             uri_builder oBuilder;
             oBuilder.set_scheme(strScheme);
             oBuilder.set_host(strHost);
             oBuilder.set_port(strPort);
 
             m_listener = new http_listener(oBuilder.to_uri());
         }
 
         m_listener->open().wait();
         m_listener->support([](http_request request)
             {
                 http_response response;
                 response.set_status_code(status_codes::ExpectationFailed);
                 request.reply(response);
              }
         );
         //-------------------------------------------------------
 
         //Hot reload hack
         FSlateStyleRegistry::UnRegisterSlateStyle(FStrategyStyle::GetStyleSetName());
         FStrategyStyle::Initialize();
     }
 
 ...

It does compile and run, though whenever a request is sent and handled, the app crashes most of the time with something like

 Exception at 0x00007FFE2FA45328 (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation writing location 0x0000000000000010

I managed to break the problem down to something related to ending threads, as cpprestsdk creates a new thread for every incoming request. when responded the thread is most likely ended, but in this scenario it seems to force the unreal engine to crash. if i'm actively preving the thread from being ending with something very bad like

                 request.reply(response);
 
                 while (true)
                 {
                     FPlatformProcess::Sleep(120.0f);
                     UE_LOG(LogTemp, Log, TEXT("do not kill request thread"));
                 }

no crashes occur, but of course that's no feasible solution at all!

Can anybody point me to the right direction or has another solution on adding a REST server to my unreal project?

Product Version: UE 4.15
Tags:
more ▼

asked May 17 '17 at 05:54 AM in C++ Programming

avatar image

fsteinke
6 1 1 1

avatar image acxsasx Jun 13 '17 at 07:05 PM

I found this and have used it with success in 4.16.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hi,

Unreal web server (http://isaratech.com/uws/) is an upcoming plugin with RESTFul compliance.

Have fun ;)

more ▼

answered Jun 13 '17 at 09:24 AM

avatar image

IsaraTech.
8 1 1 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question