A simple HTTP server for Unity3D.
- Register endpoints and serve requests with ease.
- Respond to requests from Unity's main thread, from a new thread, or immediately.
- The implementation builds on C# HttpListener.
- You can add a dependency to your
Packages/manifest.json
using a Git URL in the following form:"com.achimmihca.simplehttpserverforunity": "https://github.com/achimmihca/SimpleHttpServerForUnity.git?path=SimpleHttpServerForUnity/Packages/com.achimmihca.simplehttpserverforunity#v1.0.0"
- Note that
#v1.0.0
can be used to specify a tag or commit hash.
- Note that
- This package requires
Api Compatibility Level
.NET 4.x
or above (in UnityFile > Project Settings... > Player > Other settings
) - This package ships with a sample that can be imported to your project using Unity's Package Manager.
-
Add an instance of HttpServer component to your scene.
-
Set host and port
- Note: You can find the IP address of the current device using IpAddressUtils.
httpServer.port = 1234;
httpServer.host = IpAddressUtils.GetIpAddress(AddressFamily.IPv4, NetworkInterfaceType.Wireless80211);
// Matches a GET request on '/hello/Alice' for example
httpServer.On(HttpMethod.Get, "/hello/{name}")
.WithDescription("Say hello to someone") // Optionally, add a description
.OnThread(ResponseThread.MainThread) // Optionally, handle requests on the main thread, a new thread, or immediately
.UntilDestroy(gameObject) // Optionally, remove endpoint on destroy of some GameObject
.Do(requestData => HandleHelloRequest(requestData));
// Matches a GET request on '/files/folder/subfolder/image.png' for example
httpServer.On(HttpMethod.Get, "/files/*")
.Do(requestData => HandleFileRequest(requestData));
httpServer.StartHttpListener();
List<string> endpointInfos = httpServer.GetRegisteredEndpoints()
.Select(endpoint => $"{endpoint.HttpMethod} {endpoint.PathPattern} - {endpoint.Description}")
.ToList();
Debug.Log("Registered endpoints:\n" + string.Join("\n", endpointInfos));
SimpleHttpServerForUnity has been created originally for UltraStar Play to serve requests from a handful of clients in the same local area network. If you like singing, karaoke, or sing-along games then go check it out ;)