Guest User

Untitled

a guest
Jul 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. //using HUX.Interaction;
  9. //using HUX.Receivers;
  10. using UnityEngine.UI;
  11.  
  12. //<JEM>Ignore unity editor and run this code in the hololens instead</JEM>
  13. #if !UNITY_EDITOR
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using Windows.Networking;
  17. using Windows.Networking.Sockets;
  18. using Windows.Networking.Connectivity;
  19. using Windows.Storage.Streams;
  20. #endif
  21.  
  22. //Able to act as a reciever
  23. public class ImageReciever : MonoBehaviour
  24. {
  25. Stream streamIn;
  26. Renderer rend;
  27. Texture2D texture;
  28. int counter = 0;
  29. byte[] byArray;
  30. #if !UNITY_EDITOR
  31. StreamSocket socket;
  32. StreamSocketListener listener;
  33. String port;
  34. String message;
  35. #endif
  36.  
  37. // Use this for initialization
  38. void Start()
  39. {
  40. #if !UNITY_EDITOR
  41. rend = this.GetComponent<Renderer>();
  42. listener = new StreamSocketListener();
  43. port = "8080";
  44.  
  45. listener.ConnectionReceived += _receiver_socket_ConnectionReceived;
  46. listener.Control.KeepAlive = false;
  47.  
  48. Listener_Start();
  49. #endif
  50. }
  51.  
  52. #if !UNITY_EDITOR
  53. private async void Listener_Start()
  54. {
  55. Debug.Log("Listener started");
  56. try
  57. {
  58. await listener.BindServiceNameAsync(port);
  59. }
  60. catch (Exception e)
  61. {
  62. Debug.Log("Error: " + e.Message);
  63. }
  64.  
  65. Debug.Log("Listening");
  66. }
  67.  
  68.  
  69.  
  70. private async void _receiver_socket_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
  71. {
  72. try
  73. {
  74. while (true)
  75. {
  76. using (var dw = new DataWriter(args.Socket.OutputStream))
  77. {
  78. dw.WriteString("Hello There from C#");
  79. await dw.StoreAsync();
  80. dw.DetachStream();
  81. }
  82. using (var reader = new DataReader(args.Socket.InputStream))
  83.  
  84. {
  85.  
  86. reader.InputStreamOptions = InputStreamOptions.Partial;
  87.  
  88. uint numFileBytes = await reader.LoadAsync(reader.UnconsumedBufferLength);
  89.  
  90. byArray = new byte[numFileBytes];
  91.  
  92. reader.ReadBytes(byArray);
  93.  
  94. }
  95.  
  96. }
  97. } catch (Exception e)
  98. {
  99. Debug.Log("disconnected!!!!!!!! " + e);
  100. }
  101. }
  102.  
  103.  
  104. #endif
  105.  
  106.  
  107.  
  108. //Do nothing every frame
  109. void Update()
  110. {
  111. File.WriteAllBytes("Image.txt", byArray);
  112. texture.LoadImage(byArray);
  113. this.GetComponent<Renderer>().material.mainTexture = texture;
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment