-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer3.cs
More file actions
157 lines (132 loc) · 6.22 KB
/
WebServer3.cs
File metadata and controls
157 lines (132 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Collections;
using System.Collections.Generic;
using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
public class WebServer3 : MonoBehaviour
{
public List<GameObject> ObjectList;
private Vector3 rotationData;
// public Toggle OnOff;
HttpListener listener = new HttpListener();
List<string> cachedTransform = new List<string>();
List<string> cachedRotation = new List<string>();
string responseString = String.Empty;
string responseStringR = String.Empty;
// Start is called before the first frame update
void Start()
{
Debug.Log(ObjectList.Count);
for (int i = 0; i < ObjectList.Count; i++)
{
cachedTransform.Add($"{i}");
cachedRotation.Add($"{i}");
}
Debug.Log(cachedRotation);
foreach (GameObject givenObject in ObjectList)
{
transform.position = givenObject.transform.position;
rotationData = givenObject.transform.rotation.eulerAngles;
// Create a listener.
// Add the prefixes.
string s = "http://localhost:8080/";
listener.Prefixes.Add(s);
listener.Start();
//Translation
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response givenObject.
HttpListenerResponse response = context.Response;
// Construct a response.
responseString = "translation [" + $"{givenObject.name}" +"] " + $"{transform.position}";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
//Rotation
HttpListenerContext contextR = listener.GetContext();
HttpListenerRequest requestR = contextR.Request;
// Obtain a response givenObject.
HttpListenerResponse responseR = contextR.Response;
// Construct a response.
responseStringR = "rotation [" + $"{givenObject.name}" +"] " + $"{rotationData}";
byte[] bufferR = System.Text.Encoding.UTF8.GetBytes(responseStringR);
// Get a response stream and write the response to it.
responseR.ContentLength64 = bufferR.Length;
System.IO.Stream outputR = responseR.OutputStream;
outputR.Write(bufferR, 0, bufferR.Length);
// You must close the output stream.
outputR.Close();
Debug.Log("Here");
}
}
// Update is called once per frame
void Update()
{
//check for every object in the array object list if its coordinates has changed
for (int i = 0; i < ObjectList.Count; i++)
{
//set givenObject to current object of the list
GameObject givenObject = ObjectList[i];
//check transformation and rotation and give back a string
string transformD = UpdateTranslation(givenObject);
string rotation = UpdateRotation(givenObject);
//if transformation has changed, then send that to the listener
if (transformD != cachedTransform[i])
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response givenObject.
HttpListenerResponse response = context.Response;
responseString = "translation [" + $"{givenObject.name}" +"] " + $"{transform.position}";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
//set cachedTransformation to sent transformation
cachedTransform[i] = transformD;
}
//if transformation has changed, then send that to the listener
if (rotation != cachedRotation[i])
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response givenObject.
HttpListenerResponse response = context.Response;
responseString = "rotation [" + $"{givenObject.name}" +"] " + $"{rotationData}";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
//set cachedRotation to sent rotation
cachedRotation[i] = rotation;
}
}
}
string UpdateTranslation(GameObject givenObject)
{
transform.position = givenObject.transform.position;
//Debug.Log("translation [" + $"{givenObject.name}" + "] " + $"{transform.position}");
return "translation [" + $"{givenObject.name}" + "] " + $"{transform.position}";
}
string UpdateRotation(GameObject givenObject)
{
rotationData = givenObject.transform.rotation.eulerAngles;
//Debug.Log("rotation [" + $"{givenObject.name}" +"] " + $"{rotationData}");
return "rotation [" + $"{givenObject.name}" +"] " + $"{rotationData}";
}
}