Advertisement
Guest User

HololensServer Unity Script

a guest
Jul 18th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. #if !UNITY_EDITOR
  8.     using Windows.Networking;
  9.     using Windows.Networking.Sockets;
  10.     using Windows.Storage.Streams;
  11. #endif
  12.  
  13. //Able to receive incoming TCP messages
  14. //Tested by sending data from a python server to the hololens
  15. //To use, attach to a 3D Text game object
  16. public class HoloServer : MonoBehaviour
  17. {
  18.     public String _input = "Waiting";
  19.  
  20. #if !UNITY_EDITOR
  21.         StreamSocket socket;
  22.         StreamSocketListener listener;
  23.         String port;
  24.         String message;
  25. #endif
  26.  
  27.     // Use this for initialization
  28.     void Start()
  29.     {
  30. #if !UNITY_EDITOR
  31.         listener = new StreamSocketListener();
  32.         port = "9090";
  33.         listener.ConnectionReceived += Listener_ConnectionReceived;
  34.         listener.Control.KeepAlive = false;
  35.  
  36.         Listener_Start();
  37. #endif
  38.     }
  39.  
  40. #if !UNITY_EDITOR
  41.     private async void Listener_Start()
  42.     {
  43.         Debug.Log("Listener started");
  44.         try
  45.         {
  46.             await listener.BindServiceNameAsync(port);
  47.         }
  48.         catch (Exception e)
  49.         {
  50.             Debug.Log("Error: " + e.Message);
  51.         }
  52.  
  53.         Debug.Log("Listening");
  54.     }
  55.  
  56.     private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
  57.     {
  58.         Debug.Log("Connection received");
  59.  
  60.         try
  61.         {
  62.             while (true) {
  63.  
  64.                     using (var dw = new DataWriter(args.Socket.OutputStream))
  65.                     {
  66.                         dw.WriteString("Hello There");
  67.                         await dw.StoreAsync();
  68.                         dw.DetachStream();
  69.                     }  
  70.  
  71.                     using (var dr = new DataReader(args.Socket.InputStream))
  72.                     {
  73.                         dr.InputStreamOptions = InputStreamOptions.Partial;
  74.                         await dr.LoadAsync(12);
  75.                         var input = dr.ReadString(12);
  76.                         Debug.Log("received: " + input);
  77.                         _input = input;
  78.  
  79.                     }
  80.             }
  81.         }
  82.         catch (Exception e)
  83.         {
  84.             Debug.Log("disconnected!!!!!!!! " + e);
  85.         }
  86.  
  87.     }
  88.  
  89. #endif
  90.  
  91.     void Update()
  92.     {
  93.         this.GetComponent<TextMesh>().text = _input;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement