TinyHttpServer is a simple HTTP server implemented in C#. It uses the HttpListener class to listen for HTTP requests and handle them asynchronously.
-
GET and POST Request Handling: The server can handle GET and POST requests.
-
Customizable Port and Document Root: You can set the port and document root for the server.
-
Delegates for Data Processing: You can assign delegates to process the received text and binary data from POST requests.
To install the TinyHttpServer package using OpenUPM, run the following command in your project’s root folder:
openupm add com.afjk.tinyhttpserver
To install the TinyHttpServer package using Unity Package Manager, follow these steps:
- Open your project’s manifest.json file.
- Add the following scoped registry configuration:
"scopedRegistries": [
{
"name": "afjk's UPM registory",
"url": "https://upm.afjk.jp",
"scopes": [
"com.afjk"
]
}
]
- Save the file.
- In UnityEditor, go to Window > Package Manager.
- Select the My Registries section in the left panel.
- Search for TinyHttpServerForUnity and install it.
Alternatively, you can configure the registry via Project Settings:
- In UnityEditor, go to Edit > Project Settings > Package Manager.
- Under Scoped Registries, add the following details:
- Name: afjk’s UPM registory
- URL: https://upm.afjk.jp
- Scopes: com.afjk
You can now find and install TinyHttpServerForUnity in the Package Manager.
Alternatively, to install the TinyHttpServer package using Unity Package Manager, follow these steps:
- Open the Unity Editor and go to Window -> Package Manager.
- Click the + button in the top-left corner.
- Select Add package from git URL....
- Enter the following URL to install the latest version:
https://github.com/afjk/TinyHttpServerForUnity.git?path=/Packages/com.afjk.tinyhttpserver#v0.0.1
After installing the TinyHttpServer package, you can use the built-in Editor extension to launch a sample HTTP server:
- Open the Unity Editor and go to the menu bar.
- Navigate to Tools -> Tiny HTTP Server to open the server control panel.
- In the Port Number field, specify the desired port (e.g., 8080).
- Click the Start Server button to launch the HTTP server.
Verifying the Server
Once the server is started, the control panel will display the Server URL. You can:
- Copy URL: Copy the server URL to your clipboard.
- Open in Browser: Launch your default web browser to view the server’s content.
- Open Document Folder: Open the folder that serves as the document root for the server.
The attached screenshots below show the sample UI for starting and managing the server:
To use the server, you need to create an instance of the TinyHttpServer class, define your routes and start the server. Here is a basic example:
var server = new TinyHttpServer();
server.Port = 8080;
server.DocumentRoot = Path.Combine(Application.dataPath, "..", "html");
server.AddGetRoute("/example", HandleExampleGet);
server.AddPostRoute("/example", HandleExamplePost);
server.StartServer();