Spiel für das Medientheater HBK Saar, Teil des Projektes "Fun Palace" (Reupload der Repository von 2017)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

networkSocket.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.IO;
  5. using System.Net.Sockets;
  6. public class networkSocket : MonoBehaviour
  7. {
  8. public String host = "localhost";
  9. public Int32 port = 50000;
  10. internal Boolean socket_ready = false;
  11. internal String input_buffer = "";
  12. TcpClient tcp_socket;
  13. NetworkStream net_stream;
  14. StreamWriter socket_writer;
  15. void Update()
  16. {
  17. try{
  18. String LaserPointPos = ViveLaserPointer.sendMeToP5Bitch;
  19. writeSocket(LaserPointPos);
  20. if (!socket_ready)
  21. {
  22. Debug.Log("Trying to reconnect..");
  23. closeSocket();
  24. setupSocket();
  25. }
  26. } catch (Exception e) {
  27. socket_ready = false;
  28. Debug.Log(e + ", trying to reconnect..");
  29. }
  30. }
  31. void Awake()
  32. {
  33. Application.runInBackground = true;
  34. setupSocket();
  35. }
  36. void OnApplicationQuit()
  37. {
  38. closeSocket();
  39. }
  40. public void setupSocket()
  41. {
  42. try
  43. {
  44. tcp_socket = new TcpClient(host, port);
  45. net_stream = tcp_socket.GetStream();
  46. socket_writer = new StreamWriter(net_stream);
  47. socket_ready = true;
  48. Debug.Log("Connected");
  49. }
  50. catch (Exception e)
  51. {
  52. Debug.Log("Socket error: " + e);
  53. }
  54. }
  55. public void writeSocket(string line)
  56. {
  57. if (!socket_ready)
  58. return;
  59. line = line + "\r\n";
  60. socket_writer.Write(line);
  61. socket_writer.Flush();
  62. }
  63. public void closeSocket()
  64. {
  65. if (!socket_ready)
  66. return;
  67. socket_writer.Close();
  68. tcp_socket.Close();
  69. socket_ready = false;
  70. }
  71. }