-
-
Save frozenfoxx/a2c628f98bdae7da75b035ae9a0034c3 to your computer and use it in GitHub Desktop.
Listen for Http Requests with Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Networking; | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Threading; | |
public class UnityHttpListener : MonoBehaviour | |
{ | |
private HttpListener listener; | |
private Thread listenerThread; | |
void Start () | |
{ | |
listener = new HttpListener (); | |
listener.Prefixes.Add ("http://localhost:4444/"); | |
listener.Prefixes.Add ("http://127.0.0.1:4444/"); | |
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; | |
listener.Start (); | |
listenerThread = new Thread (startListener); | |
listenerThread.Start (); | |
Debug.Log ("Server Started"); | |
} | |
void Update () | |
{ | |
} | |
private void startListener () | |
{ | |
while (true) { | |
var result = listener.BeginGetContext (ListenerCallback, listener); | |
result.AsyncWaitHandle.WaitOne (); | |
} | |
} | |
private void ListenerCallback (IAsyncResult result) | |
{ | |
var context = listener.EndGetContext (result); | |
Debug.Log ("Method: " + context.Request.HttpMethod); | |
Debug.Log ("LocalUrl: " + context.Request.Url.LocalPath); | |
if (context.Request.QueryString.AllKeys.Length > 0) | |
foreach (var key in context.Request.QueryString.AllKeys) { | |
Debug.Log ("Key: " + key + ", Value: " + context.Request.QueryString.GetValues (key) [0]); | |
} | |
if (context.Request.HttpMethod == "POST") { | |
Thread.Sleep (1000); | |
var data_text = new StreamReader (context.Request.InputStream, | |
context.Request.ContentEncoding).ReadToEnd (); | |
Debug.Log (data_text); | |
} | |
context.Response.Close (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment