-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSThingy.cs
167 lines (136 loc) · 4.35 KB
/
WSThingy.cs
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
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using WebSocketSharp;
public class WSThingy : MonoBehaviour
{
public string serverUrl;
private WebSocket _ws;
private string _clientId;
public GameObject circle;
private readonly Queue<string> _messageQueue = new();
private void Start()
{
SetCircleColor(Color.white);
Debug.Log("Connecting to " + serverUrl);
_ws = new WebSocket(serverUrl);
_ws.OnMessage += OnMessage;
_ws.OnOpen += OnOpen;
_ws.OnError += OnError;
_ws.OnClose += OnClose;
_ws.Connect();
}
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
private void OnDestroy()
{
_ws?.Close();
}
private void Update()
{
while (_messageQueue.Count > 0)
{
ProcessMessage(_messageQueue.Dequeue());
}
}
private void OnOpen(object sender, System.EventArgs e)
{
Debug.Log("WebSocket connection opened");
// Set circle color to green when WebSocket is connected
SetCircleColor(Color.green);
}
private void OnMessage(object sender, MessageEventArgs e)
{
_messageQueue.Enqueue(e.Data);
}
private void OnError(object sender, ErrorEventArgs e)
{
Debug.LogError("WebSocket error: " + e.Message);
// Set circle color to red on error
SetCircleColor(Color.red);
}
private void OnClose(object sender, CloseEventArgs e)
{
Debug.Log("WebSocket connection closed with reason: " + e.Reason);
// Set circle color to red on close
SetCircleColor(Color.red);
}
private void ProcessMessage(string data)
{
Debug.Log("WebSocket message received: " + data);
// Set circle color to yellow temporarily when message is received
//parse the message as a JSON object
var json = JsonConvert.DeserializeObject<Dictionary<string, object>>(data);
Debug.Log("Thingy: " + json);
switch (json["type"])
{
case "yolo":
Debug.Log("Received yolo");
/**
* class: string;
* conf: number;
* box: list[number] (len 4)
*/
Debug.Log(json["data"]);
JArray a = (JArray)json["data"];
var b = a.ToObject(typeof(Dictionary<string, object>[]));
Dictionary<string, object>[] c = (Dictionary<string, object>[])b;
UIRenderer.detections = c;
UIRenderer.lastDetectionTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
break;
}
// SocketTransferIndicator();
}
private void SocketTransferIndicator()
{
SetCircleColor(Color.yellow);
Invoke(nameof(ResetCircleColor), 0.5f);
}
private void SetCircleColor(Color color)
{
if (circle == null)
return;
var component = circle.GetComponent<Renderer>();
if (component != null)
{
component.material.color = color;
}
}
private void ResetCircleColor()
{
SetCircleColor(_ws.IsAlive ? Color.green : Color.red);
}
// private void SendGameStart()
// {
// Debug.Log("Sending gameStart message to server");
// var message = new Dictionary<string, string>
// {
// { "type", "startGame" }, { "id", _clientId }
// };
// var json = JsonConvert.SerializeObject(message);
// _ws.Send(json);
// SocketTransferIndicator();
// }
private void SendGameEnd()
{
var message = new Dictionary<string, string> { { "type", "gameEnd" }, { "id", _clientId } };
var json = JsonConvert.SerializeObject(message);
_ws.Send(json);
SocketTransferIndicator();
}
private void SendNextLetter()
{
var message = new Dictionary<string, string>
{
{ "type", "nextLetter" },
{ "id", _clientId }
};
var json = JsonConvert.SerializeObject(message);
_ws.Send(json);
SocketTransferIndicator();
}
}