All blueprints nodes explained in the following manual can be found in UnrealWebServer category.
Get, Start, Stop server
Unreal Web Server’s main class is a Singleton (that restricts the instantiation of the UWS class to one object). You can GET this class anywhere in your blueprints using the function GetOrCreateWebServerInstance. As it name means, this function will get the UWS instance if it exists. Otherwise, this function will create it. In each case, a UWS instance is returned.
Once you have this instance, you can start it using the StartServer function. This function needs :
- Port as integer: this is the port on which UWS will start listening. By default the port is 8080 but usual web servers runs on port 80. You just need to be sure that this port is not already used (or will not be) on the computer.
- DocumentRoot as String: this is the folder path where UWS will parse your static (HTML) files. For instance, “www” path means that the files needs to be in the “www” folder in your game contents. This parameter is optional.
We strongly recommend starting the server in the level blueprint, in the BeginPlay event.
The server needs to be properly stopped. You must use the StopServer function (in the EndPlay event for instance) to do this job and avoid further issues.
At this step, you should be able to access to your UWS by navigating at http://127.0.0.1:8080 (assuming 8080 is the server port) in a web browser. Because you don’t have set any web file or URI handler, the response will be “404 Not found”.
Register URI handlers
Once the UWS is properly running, you can add URI Handlers. A URI Handler is needed to map a specific URL to a specific Unreal behavior (blueprint event).
A URI handler is registered by using the AddURIHandler function. This function needs :
- Path as String: this is the path where you want to map to a handler. For instance “/” means http://127.0.0.1:8080/ in the web browser, “/test” means http://127.0.0.1:8080/test, and so on…
- Method as EHTTPRequestMethodEnum: this is the HTTP method needed for request mapping. If you select GET method and a POST request comes, this handler will not be used. You can specify “*” in order to listen all HTTP methods.
- Callback as Delegate with a Connection parameter: this is the event fired when a connection is handled on this URL with the HTTP method handled. This event provides us a Connection object (it will be explained in a next section).
- Execute callback in game thread as boolean : if you choose to set this boolean as true, the callback will be dispatched in gamethread. Pros : you can directly modify gameobjects (such as their location…). Cons : be carefull with performance issues, if your callback needs 100ms to be computed, your frame will be freezed for 100ms at least…
Of course, you can either create a custom event or map a function to the callback, such as on the picture.
Asterisk in URI Handler’s Path
With the upgrade UWS 1.5, you can use the asterisk in the URI Handler’s path. It allows you to match any symbol or group of symbols in the URI.
For instance if you want to match every possible URL, just use “*”. If you want to match any URL under the /Group1 path, use “/Group1/*”.
To get the uri path called by the user, you can use the function Connection::GetUriPath.