-
Notifications
You must be signed in to change notification settings - Fork 171
/
UnityWebSocketDemo.cs
191 lines (168 loc) · 6.77 KB
/
UnityWebSocketDemo.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using UnityEngine;
namespace UnityWebSocket.Demo
{
public class UnityWebSocketDemo : MonoBehaviour
{
public string address = "wss://echo.websocket.events";
public string sendText = "Hello UnityWebSocket!";
private IWebSocket socket;
private bool logMessage = true;
private string log = "";
private int sendCount;
private int receiveCount;
private Vector2 scrollPos;
private Color green = new Color(0.1f, 1, 0.1f);
private Color red = new Color(1f, 0.1f, 0.1f);
private Color wait = new Color(0.7f, 0.3f, 0.3f);
private void OnGUI()
{
var scale = Screen.width / 800f;
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scale, scale, 1));
var width = GUILayout.Width(Screen.width / scale - 10);
WebSocketState state = socket == null ? WebSocketState.Closed : socket.ReadyState;
GUILayout.BeginHorizontal();
GUILayout.Label("SDK Version: " + Settings.VERSION, GUILayout.Width(Screen.width / scale - 100));
GUI.color = green;
GUILayout.Label($"FPS: {fps:F2}", GUILayout.Width(80));
GUI.color = Color.white;
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("State: ", GUILayout.Width(36));
GUI.color = WebSocketState.Closed == state ? red : WebSocketState.Open == state ? green : wait;
GUILayout.Label($"{state}", GUILayout.Width(120));
GUI.color = Color.white;
GUILayout.EndHorizontal();
GUI.enabled = state == WebSocketState.Closed;
GUILayout.Label("Address: ", width);
address = GUILayout.TextField(address, width);
GUILayout.BeginHorizontal();
GUI.enabled = state == WebSocketState.Closed;
if (GUILayout.Button(state == WebSocketState.Connecting ? "Connecting..." : "Connect"))
{
socket = new WebSocket(address);
socket.OnOpen += Socket_OnOpen;
socket.OnMessage += Socket_OnMessage;
socket.OnClose += Socket_OnClose;
socket.OnError += Socket_OnError;
AddLog(string.Format("Connecting..."));
socket.ConnectAsync();
}
GUI.enabled = state == WebSocketState.Open;
if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close"))
{
AddLog(string.Format("Closing..."));
socket.CloseAsync();
}
GUILayout.EndHorizontal();
GUILayout.Label("Message: ");
sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Send") && !string.IsNullOrEmpty(sendText))
{
socket.SendAsync(sendText);
AddLog(string.Format("Send: {0}", sendText));
sendCount += 1;
}
if (GUILayout.Button("Send Bytes") && !string.IsNullOrEmpty(sendText))
{
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
socket.SendAsync(bytes);
AddLog(string.Format("Send Bytes ({1}): {0}", sendText, bytes.Length));
sendCount += 1;
}
if (GUILayout.Button("Send x100") && !string.IsNullOrEmpty(sendText))
{
for (int i = 0; i < 100; i++)
{
var text = (i + 1).ToString() + ". " + sendText;
socket.SendAsync(text);
AddLog(string.Format("Send: {0}", text));
sendCount += 1;
}
}
if (GUILayout.Button("Send Bytes x100") && !string.IsNullOrEmpty(sendText))
{
for (int i = 0; i < 100; i++)
{
var text = (i + 1).ToString() + ". " + sendText;
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
socket.SendAsync(bytes);
AddLog(string.Format("Send Bytes ({1}): {0}", text, bytes.Length));
sendCount += 1;
}
}
GUILayout.EndHorizontal();
GUI.enabled = true;
GUILayout.BeginHorizontal();
logMessage = GUILayout.Toggle(logMessage, "Log Message");
GUILayout.Label(string.Format("Send Count: {0}", sendCount));
GUILayout.Label(string.Format("Receive Count: {0}", receiveCount));
GUILayout.EndHorizontal();
if (GUILayout.Button("Clear"))
{
log = "";
receiveCount = 0;
sendCount = 0;
}
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width);
GUILayout.Label(log);
GUILayout.EndScrollView();
}
private void AddLog(string str)
{
if (!logMessage) return;
if (str.Length > 100) str = str.Substring(0, 100) + "...";
log += str + "\n";
if (log.Length > 22 * 1024)
{
log = log.Substring(log.Length - 22 * 1024);
}
scrollPos.y = int.MaxValue;
}
private void Socket_OnOpen(object sender, OpenEventArgs e)
{
AddLog(string.Format("Connected: {0}", address));
}
private void Socket_OnMessage(object sender, MessageEventArgs e)
{
if (e.IsBinary)
{
AddLog(string.Format("Receive Bytes ({1}): {0}", e.Data, e.RawData.Length));
}
else if (e.IsText)
{
AddLog(string.Format("Receive: {0}", e.Data));
}
receiveCount += 1;
}
private void Socket_OnClose(object sender, CloseEventArgs e)
{
AddLog(string.Format("Closed: StatusCode: {0}, Reason: {1}", e.StatusCode, e.Reason));
}
private void Socket_OnError(object sender, ErrorEventArgs e)
{
AddLog(string.Format("Error: {0}", e.Message));
}
private void OnApplicationQuit()
{
if (socket != null && socket.ReadyState != WebSocketState.Closed)
{
socket.CloseAsync();
}
}
private int frame = 0;
private float time = 0;
private float fps = 0;
private void Update()
{
frame += 1;
time += Time.deltaTime;
if (time >= 0.5f)
{
fps = frame / time;
frame = 0;
time = 0;
}
}
}
}