Skip to content

Commit c57bed0

Browse files
committed
v2.8.0 性能优化及Bug修,优化部分功能。
2 parents 674f6c7 + 99da436 commit c57bed0

File tree

12 files changed

+127
-187
lines changed

12 files changed

+127
-187
lines changed

Assets/UnityWebSocket/Demo/UnityWebSocketDemo.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,28 @@ private void OnGUI()
2626

2727
WebSocketState state = socket == null ? WebSocketState.Closed : socket.ReadyState;
2828

29+
// draw header
2930
GUILayout.BeginHorizontal();
3031
GUILayout.Label("SDK Version: " + Settings.VERSION, GUILayout.Width(Screen.width / scale - 100));
3132
GUI.color = green;
3233
GUILayout.Label($"FPS: {fps:F2}", GUILayout.Width(80));
3334
GUI.color = Color.white;
3435
GUILayout.EndHorizontal();
3536

37+
// draw websocket state
3638
GUILayout.BeginHorizontal();
3739
GUILayout.Label("State: ", GUILayout.Width(36));
3840
GUI.color = WebSocketState.Closed == state ? red : WebSocketState.Open == state ? green : wait;
3941
GUILayout.Label($"{state}", GUILayout.Width(120));
4042
GUI.color = Color.white;
4143
GUILayout.EndHorizontal();
4244

45+
// draw address
4346
GUI.enabled = state == WebSocketState.Closed;
4447
GUILayout.Label("Address: ", width);
4548
address = GUILayout.TextField(address, width);
4649

50+
// draw connect button
4751
GUILayout.BeginHorizontal();
4852
GUI.enabled = state == WebSocketState.Closed;
4953
if (GUILayout.Button(state == WebSocketState.Connecting ? "Connecting..." : "Connect"))
@@ -57,6 +61,7 @@ private void OnGUI()
5761
socket.ConnectAsync();
5862
}
5963

64+
// draw close button
6065
GUI.enabled = state == WebSocketState.Open;
6166
if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close"))
6267
{
@@ -65,9 +70,11 @@ private void OnGUI()
6570
}
6671
GUILayout.EndHorizontal();
6772

73+
// draw input message
6874
GUILayout.Label("Message: ");
6975
sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width);
7076

77+
// draw send message button
7178
GUILayout.BeginHorizontal();
7279
if (GUILayout.Button("Send") && !string.IsNullOrEmpty(sendText))
7380
{
@@ -103,23 +110,25 @@ private void OnGUI()
103110
sendCount += 1;
104111
}
105112
}
106-
107113
GUILayout.EndHorizontal();
108114

115+
// draw message count
109116
GUI.enabled = true;
110117
GUILayout.BeginHorizontal();
111118
logMessage = GUILayout.Toggle(logMessage, "Log Message");
112119
GUILayout.Label(string.Format("Send Count: {0}", sendCount));
113120
GUILayout.Label(string.Format("Receive Count: {0}", receiveCount));
114121
GUILayout.EndHorizontal();
115122

123+
// draw clear button
116124
if (GUILayout.Button("Clear"))
117125
{
118126
log = "";
119127
receiveCount = 0;
120128
sendCount = 0;
121129
}
122130

131+
// draw message content
123132
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width);
124133
GUILayout.Label(log);
125134
GUILayout.EndScrollView();

Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var WebSocketLibrary =
99
* {
1010
* url: string,
1111
* ws: WebSocket,
12-
* binaryType: string,
1312
* subProtocols: string[],
1413
* }
1514
*/
@@ -21,6 +20,7 @@ var WebSocketLibrary =
2120
/* Event listeners */
2221
onOpen: null,
2322
onMessage: null,
23+
onMessageStr: null,
2424
onError: null,
2525
onClose: null
2626
},
@@ -46,7 +46,7 @@ var WebSocketLibrary =
4646
},
4747

4848
/**
49-
* Set onMessage callback
49+
* Set onMessageStr callback
5050
*
5151
* @param callback Reference to C# static function
5252
*/
@@ -80,15 +80,13 @@ var WebSocketLibrary =
8080
*
8181
* @param url Server URL
8282
*/
83-
WebSocketAllocate: function(urlPtr, binaryTypePtr)
83+
WebSocketAllocate: function(urlPtr)
8484
{
8585
var url = UTF8ToString(urlPtr);
86-
var binaryType = UTF8ToString(binaryTypePtr);
8786
var id = ++webSocketManager.lastId;
8887
webSocketManager.instances[id] = {
8988
url: url,
9089
ws: null,
91-
binaryType: binaryType
9290
};
9391

9492
return id;
@@ -107,7 +105,7 @@ var WebSocketLibrary =
107105

108106
var protocol = UTF8ToString(protocolPtr);
109107

110-
if(instance.subProtocols == null)
108+
if (instance.subProtocols == null)
111109
instance.subProtocols = [];
112110

113111
instance.subProtocols.push(protocol);
@@ -149,13 +147,11 @@ var WebSocketLibrary =
149147
if (!instance) return -1;
150148
if (instance.ws !== null) return -2;
151149

152-
if(instance.subProtocols != null)
150+
if (instance.subProtocols != null)
153151
instance.ws = new WebSocket(instance.url, instance.subProtocols);
154152
else
155153
instance.ws = new WebSocket(instance.url);
156154

157-
instance.ws.binaryType = instance.binaryType;
158-
159155
instance.ws.onopen = function()
160156
{
161157
Module.dynCall_vi(webSocketManager.onOpen, instanceId);
@@ -177,7 +173,21 @@ var WebSocketLibrary =
177173
_free(buffer);
178174
}
179175
}
180-
else if (ev.data instanceof Blob)
176+
else if (typeof ev.data == 'string')
177+
{
178+
var length = lengthBytesUTF8(ev.data) + 1;
179+
var buffer = _malloc(length);
180+
stringToUTF8(ev.data, buffer, length);
181+
try
182+
{
183+
Module.dynCall_vii(webSocketManager.onMessageStr, instanceId, buffer);
184+
}
185+
finally
186+
{
187+
_free(buffer);
188+
}
189+
}
190+
else if (typeof Blob !== 'undefined' && ev.data instanceof Blob)
181191
{
182192
var reader = new FileReader();
183193
reader.onload = function()
@@ -197,20 +207,6 @@ var WebSocketLibrary =
197207
};
198208
reader.readAsArrayBuffer(ev.data);
199209
}
200-
else if(typeof ev.data == 'string')
201-
{
202-
var length = lengthBytesUTF8(ev.data) + 1;
203-
var buffer = _malloc(length);
204-
stringToUTF8(ev.data, buffer, length);
205-
try
206-
{
207-
Module.dynCall_vii(webSocketManager.onMessageStr, instanceId, buffer);
208-
}
209-
finally
210-
{
211-
_free(buffer);
212-
}
213-
}
214210
else
215211
{
216212
console.log("[JSLIB WebSocket] not support message type: ", (typeof ev.data));
@@ -273,7 +269,7 @@ var WebSocketLibrary =
273269
{
274270
instance.ws.close(code, reason);
275271
}
276-
catch(err)
272+
catch (err)
277273
{
278274
return -7;
279275
}
@@ -295,7 +291,12 @@ var WebSocketLibrary =
295291
if (instance.ws === null) return -3;
296292
if (instance.ws.readyState !== 1) return -6;
297293

298-
instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length));
294+
if (typeof HEAPU8 !== 'undefined')
295+
instance.ws.send(HEAPU8.buffer.slice(bufferPtr, bufferPtr + length));
296+
else if (typeof buffer !== 'undefined')
297+
instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length));
298+
else
299+
return -8; // not support buffer slice
299300

300301
return 0;
301302
},

Assets/UnityWebSocket/Scripts/Editor/SettingsWindow.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,12 @@ private void DrawVersion()
9393
{
9494
if (GUI.Button(new Rect(440, 50, 150, 18), "Update to | " + latestVersion))
9595
{
96-
ShowUpdateDialog();
96+
Application.OpenURL(Settings.GITHUB + "/releases");
9797
}
9898
}
9999
}
100100
}
101101

102-
private void ShowUpdateDialog()
103-
{
104-
var isOK = EditorUtility.DisplayDialog("UnityWebSocket",
105-
"Update UnityWebSocket now?\n" + changeLog,
106-
"Update Now", "Cancel");
107-
108-
if (isOK)
109-
{
110-
UpdateVersion();
111-
}
112-
}
113-
114-
private void UpdateVersion()
115-
{
116-
Application.OpenURL(Settings.GITHUB + "/releases");
117-
}
118-
119102
private void DrawHelper()
120103
{
121104
GUI.Label(new Rect(330, 200, 100, 18), "GitHub:", TextStyle(10, TextAnchor.MiddleRight));

Assets/UnityWebSocket/Scripts/Runtime/Core/IWebSocket.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace UnityWebSocket
44
{
@@ -120,19 +120,6 @@ public interface IWebSocket
120120
/// </value>
121121
WebSocketState ReadyState { get; }
122122

123-
/// <summary>
124-
/// Gets the current binaryType of the connection, supported on WEBGL platform only.
125-
/// </summary>
126-
/// <value>
127-
/// <para>
128-
/// It indicates the current binaryType of the connection.
129-
/// </para>
130-
/// <para>
131-
/// The default value is "arraybuffer", options: "blob" or "arraybuffer".
132-
/// </para>
133-
/// </value>
134-
string BinaryType { get; set; }
135-
136123
/// <summary>
137124
/// Occurs when the WebSocket connection has been established.
138125
/// </summary>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace UnityWebSocket
1+
namespace UnityWebSocket
22
{
33
public static class Settings
44
{
@@ -7,6 +7,6 @@ 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.7.0";
10+
public const string VERSION = "2.8.0";
1111
}
1212
}

0 commit comments

Comments
 (0)