Skip to content

Commit 4f62723

Browse files
author
yinlong
committed
2.5.1
1 parent 80d925c commit 4f62723

File tree

2 files changed

+139
-136
lines changed

2 files changed

+139
-136
lines changed
Lines changed: 137 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
using UnityEngine;
1+
using UnityEngine;
22
using UnityWebSocket;
33

4-
public class UnityWebSocketDemo : MonoBehaviour
4+
namespace UnityWebSocket.Demo
55
{
6-
public string address = "ws://127.0.0.1";
7-
public string sendText = "Hello World!";
8-
public bool logMessage = true;
6+
public class UnityWebSocketDemo : MonoBehaviour
7+
{
8+
public string address = "ws://127.0.0.1";
9+
public string sendText = "Hello World!";
10+
public bool logMessage = true;
911

10-
private IWebSocket socket;
12+
private IWebSocket socket;
1113

12-
private string log = "";
13-
private int sendCount;
14-
private int receiveCount;
15-
private Vector2 scrollPos;
14+
private string log = "";
15+
private int sendCount;
16+
private int receiveCount;
17+
private Vector2 scrollPos;
1618

1719
#if !UNITY_EDITOR && UNITY_WEBGL
1820
private void Awake()
@@ -21,166 +23,167 @@ private void Awake()
2123
}
2224
#endif
2325

24-
private void OnGUI()
25-
{
26-
var scale = Screen.width / 800f;
27-
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scale, scale, 1));
28-
var width = GUILayout.Width(Screen.width / scale - 10);
29-
30-
WebSocketState state = socket == null ? WebSocketState.Closed : socket.ReadyState;
31-
32-
GUILayout.Label("SDK Version: 2.5.0", width);
33-
var stateColor = state == WebSocketState.Closed ? "red" : state == WebSocketState.Open ? "#11ff11" : "#aa4444";
34-
var richText = new GUIStyle() { richText = true };
35-
GUILayout.Label(string.Format(" <color=white>State:</color> <color={1}>{0}</color>", state, stateColor), richText);
36-
37-
GUI.enabled = state == WebSocketState.Closed;
38-
GUILayout.Label("Address: ", width);
39-
address = GUILayout.TextField(address, width);
40-
41-
GUILayout.BeginHorizontal();
42-
GUI.enabled = state == WebSocketState.Closed;
43-
if (GUILayout.Button(state == WebSocketState.Connecting ? "Connecting..." : "Connect"))
26+
private void OnGUI()
4427
{
45-
socket = new WebSocket(address);
46-
socket.OnOpen += Socket_OnOpen;
47-
socket.OnMessage += Socket_OnMessage;
48-
socket.OnClose += Socket_OnClose;
49-
socket.OnError += Socket_OnError;
50-
AddLog(string.Format("Connecting...\n"));
51-
socket.ConnectAsync();
52-
}
28+
var scale = Screen.width / 800f;
29+
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scale, scale, 1));
30+
var width = GUILayout.Width(Screen.width / scale - 10);
5331

54-
GUI.enabled = state == WebSocketState.Open;
55-
if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close"))
56-
{
57-
AddLog(string.Format("Closing...\n"));
58-
socket.CloseAsync();
59-
}
60-
GUILayout.EndHorizontal();
32+
WebSocketState state = socket == null ? WebSocketState.Closed : socket.ReadyState;
6133

62-
GUILayout.Label("Text: ");
63-
sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width);
34+
GUILayout.Label("SDK Version: 2.5.0", width);
35+
var stateColor = state == WebSocketState.Closed ? "red" : state == WebSocketState.Open ? "#11ff11" : "#aa4444";
36+
var richText = new GUIStyle() { richText = true };
37+
GUILayout.Label(string.Format(" <color=white>State:</color> <color={1}>{0}</color>", state, stateColor), richText);
6438

65-
GUILayout.BeginHorizontal();
66-
if (GUILayout.Button("Send"))
67-
{
68-
if (!string.IsNullOrEmpty(sendText))
39+
GUI.enabled = state == WebSocketState.Closed;
40+
GUILayout.Label("Address: ", width);
41+
address = GUILayout.TextField(address, width);
42+
43+
GUILayout.BeginHorizontal();
44+
GUI.enabled = state == WebSocketState.Closed;
45+
if (GUILayout.Button(state == WebSocketState.Connecting ? "Connecting..." : "Connect"))
6946
{
70-
socket.SendAsync(sendText);
71-
if (logMessage)
72-
AddLog(string.Format("Send: {0}\n", sendText));
73-
sendCount += 1;
47+
socket = new WebSocket(address);
48+
socket.OnOpen += Socket_OnOpen;
49+
socket.OnMessage += Socket_OnMessage;
50+
socket.OnClose += Socket_OnClose;
51+
socket.OnError += Socket_OnError;
52+
AddLog(string.Format("Connecting...\n"));
53+
socket.ConnectAsync();
7454
}
75-
}
76-
if (GUILayout.Button("Send Bytes"))
77-
{
78-
if (!string.IsNullOrEmpty(sendText))
79-
{
80-
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
81-
socket.SendAsync(bytes);
8255

83-
if (logMessage)
84-
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
85-
sendCount += 1;
56+
GUI.enabled = state == WebSocketState.Open;
57+
if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close"))
58+
{
59+
AddLog(string.Format("Closing...\n"));
60+
socket.CloseAsync();
8661
}
87-
}
88-
if (GUILayout.Button("Send x100"))
89-
{
90-
if (!string.IsNullOrEmpty(sendText))
62+
GUILayout.EndHorizontal();
63+
64+
GUILayout.Label("Text: ");
65+
sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width);
66+
67+
GUILayout.BeginHorizontal();
68+
if (GUILayout.Button("Send"))
9169
{
92-
for (int i = 0; i < 100; i++)
70+
if (!string.IsNullOrEmpty(sendText))
9371
{
94-
var text = (i + 1).ToString() + ". " + sendText;
95-
socket.SendAsync(text);
96-
72+
socket.SendAsync(sendText);
9773
if (logMessage)
98-
AddLog(string.Format("Send: {0}\n", text));
74+
AddLog(string.Format("Send: {0}\n", sendText));
9975
sendCount += 1;
10076
}
10177
}
102-
}
103-
if (GUILayout.Button("Send Bytes x100"))
104-
{
105-
if (!string.IsNullOrEmpty(sendText))
78+
if (GUILayout.Button("Send Bytes"))
10679
{
107-
for (int i = 0; i < 100; i++)
80+
if (!string.IsNullOrEmpty(sendText))
10881
{
109-
var text = (i + 1).ToString() + ". " + sendText;
110-
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
82+
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
11183
socket.SendAsync(bytes);
84+
11285
if (logMessage)
113-
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
86+
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
11487
sendCount += 1;
11588
}
11689
}
117-
}
118-
GUILayout.EndHorizontal();
90+
if (GUILayout.Button("Send x100"))
91+
{
92+
if (!string.IsNullOrEmpty(sendText))
93+
{
94+
for (int i = 0; i < 100; i++)
95+
{
96+
var text = (i + 1).ToString() + ". " + sendText;
97+
socket.SendAsync(text);
98+
99+
if (logMessage)
100+
AddLog(string.Format("Send: {0}\n", text));
101+
sendCount += 1;
102+
}
103+
}
104+
}
105+
if (GUILayout.Button("Send Bytes x100"))
106+
{
107+
if (!string.IsNullOrEmpty(sendText))
108+
{
109+
for (int i = 0; i < 100; i++)
110+
{
111+
var text = (i + 1).ToString() + ". " + sendText;
112+
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
113+
socket.SendAsync(bytes);
114+
if (logMessage)
115+
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
116+
sendCount += 1;
117+
}
118+
}
119+
}
120+
GUILayout.EndHorizontal();
119121

120-
GUI.enabled = true;
121-
GUILayout.BeginHorizontal();
122-
logMessage = GUILayout.Toggle(logMessage, "Log Message");
123-
GUILayout.Label(string.Format("Send Count: {0}", sendCount));
124-
GUILayout.Label(string.Format("Receive Count: {0}", receiveCount));
125-
GUILayout.EndHorizontal();
122+
GUI.enabled = true;
123+
GUILayout.BeginHorizontal();
124+
logMessage = GUILayout.Toggle(logMessage, "Log Message");
125+
GUILayout.Label(string.Format("Send Count: {0}", sendCount));
126+
GUILayout.Label(string.Format("Receive Count: {0}", receiveCount));
127+
GUILayout.EndHorizontal();
126128

127-
if (GUILayout.Button("Clear"))
128-
{
129-
log = "";
130-
receiveCount = 0;
131-
sendCount = 0;
132-
}
129+
if (GUILayout.Button("Clear"))
130+
{
131+
log = "";
132+
receiveCount = 0;
133+
sendCount = 0;
134+
}
133135

134-
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width);
135-
GUILayout.Label(log);
136-
GUILayout.EndScrollView();
137-
}
136+
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width);
137+
GUILayout.Label(log);
138+
GUILayout.EndScrollView();
139+
}
138140

139-
private void AddLog(string str)
140-
{
141-
log += str;
142-
// max log
143-
if (log.Length > 32 * 1024)
141+
private void AddLog(string str)
144142
{
145-
log = log.Substring(16 * 1024);
143+
log += str;
144+
// max log
145+
if (log.Length > 32 * 1024)
146+
{
147+
log = log.Substring(16 * 1024);
148+
}
146149
}
147-
}
148-
149-
private void Socket_OnOpen(object sender, OpenEventArgs e)
150-
{
151-
AddLog(string.Format("Connected: {0}\n", address));
152-
}
153150

154-
private void Socket_OnMessage(object sender, MessageEventArgs e)
155-
{
156-
if (e.IsBinary)
151+
private void Socket_OnOpen(object sender, OpenEventArgs e)
157152
{
158-
if (logMessage)
159-
AddLog(string.Format("Receive Bytes ({1}): {0}\n", e.Data, e.RawData.Length));
153+
AddLog(string.Format("Connected: {0}\n", address));
160154
}
161-
else if (e.IsText)
155+
156+
private void Socket_OnMessage(object sender, MessageEventArgs e)
162157
{
163-
if (logMessage)
164-
AddLog(string.Format("Receive: {0}\n", e.Data));
158+
if (e.IsBinary)
159+
{
160+
if (logMessage)
161+
AddLog(string.Format("Receive Bytes ({1}): {0}\n", e.Data, e.RawData.Length));
162+
}
163+
else if (e.IsText)
164+
{
165+
if (logMessage)
166+
AddLog(string.Format("Receive: {0}\n", e.Data));
167+
}
168+
receiveCount += 1;
165169
}
166-
receiveCount += 1;
167-
}
168170

169-
private void Socket_OnClose(object sender, CloseEventArgs e)
170-
{
171-
AddLog(string.Format("Closed: StatusCode: {0}, Reason: {1}\n", e.StatusCode, e.Reason));
172-
}
171+
private void Socket_OnClose(object sender, CloseEventArgs e)
172+
{
173+
AddLog(string.Format("Closed: StatusCode: {0}, Reason: {1}\n", e.StatusCode, e.Reason));
174+
}
173175

174-
private void Socket_OnError(object sender, ErrorEventArgs e)
175-
{
176-
AddLog(string.Format("Error: {0}\n", e.Message));
177-
}
176+
private void Socket_OnError(object sender, ErrorEventArgs e)
177+
{
178+
AddLog(string.Format("Error: {0}\n", e.Message));
179+
}
178180

179-
private void OnApplicationQuit()
180-
{
181-
if (socket != null && socket.ReadyState != WebSocketState.Closed)
181+
private void OnApplicationQuit()
182182
{
183-
socket.CloseAsync();
183+
if (socket != null && socket.ReadyState != WebSocketState.Closed)
184+
{
185+
socket.CloseAsync();
186+
}
184187
}
185188
}
186189
}

Assets/UnityWebSocket/Scripts/Editor/Settings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace UnityWebSocket.Editor
1+
namespace UnityWebSocket.Editor
22
{
33
public static class Settings
44
{
@@ -7,7 +7,7 @@ public static class Settings
77
public const string QQ_GROUP_LINK = "https://qm.qq.com/cgi-bin/qm/qr?k=KcexYJ9aYwogFXbj2aN0XHH5b2G7ICmd";
88
public const string EMAIL = "[email protected]";
99
public const string AUHTOR = "psygames";
10-
public const string VERSION = "2.5.0";
10+
public const string VERSION = "2.5.1";
1111
public const string PACKAGE_NAME = "com.psygames.unitywebsocket";
1212
public const string UPM_URL = "https://github.com/psygames/UnityWebSocket.git";
1313
}

0 commit comments

Comments
 (0)