Advertisement
horato

listener

Mar 12th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public MainWindow()
  2. {
  3. _listener = new HttpListener();
  4. _listener.Prefixes.Add("http://127.0.0.1:60024/");
  5. _listener.Start();
  6. _listener.BeginGetContext(new AsyncCallback(ProcessRequest), null);
  7. }
  8. private void ProcessRequest(IAsyncResult result)
  9. {
  10. HttpListenerContext context = _listener.EndGetContext(result);
  11. HttpListenerRequest request = context.Request;
  12.  
  13. var sr = new StreamReader(request.InputStream);
  14. var command = sr.ReadToEnd();
  15.  
  16. string responseString;
  17. if (command != "action=getCommand")
  18. {
  19. command = command.Split(new string[] { "data=" }, StringSplitOptions.None)[1];
  20. System.Diagnostics.Debug.WriteLine(command);
  21. responseString = "null";
  22. }
  23. else
  24. responseString = "API.getVolume()";
  25.  
  26. HttpListenerResponse response = context.Response;
  27. response.ContentType = "text/html";
  28. response.Headers.Add("Access-Control-Allow-Origin", "*");
  29. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  30. response.ContentLength64 = buffer.Length;
  31. Stream output = response.OutputStream;
  32.  
  33. output.Write(buffer, 0, buffer.Length);
  34. output.Close();
  35. _listener.BeginGetContext(new AsyncCallback(ProcessRequest), null);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement