Skip to content

Commit ce7cf5b

Browse files
author
psygames
committed
websocket js support for wegame & code optimize
1 parent 9f02edc commit ce7cf5b

File tree

6 files changed

+77
-63
lines changed

6 files changed

+77
-63
lines changed

Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var WebSocketLibrary =
99
* {
1010
* url: string,
1111
* ws: WebSocket,
12-
* subProtocols: string[],
12+
* binaryType: string,
13+
* subProtocols: string[],
1314
* }
1415
*/
1516
instances: {},
@@ -79,14 +80,17 @@ var WebSocketLibrary =
7980
*
8081
* @param url Server URL
8182
*/
82-
WebSocketAllocate: function(url)
83+
WebSocketAllocate: function(urlPtr, binaryTypePtr)
8384
{
84-
var urlStr = UTF8ToString(url);
85+
var url = UTF8ToString(urlPtr);
86+
var binaryType = UTF8ToString(binaryTypePtr);
8587
var id = ++webSocketManager.lastId;
8688
webSocketManager.instances[id] = {
87-
url: urlStr,
88-
ws: null
89+
url: url,
90+
ws: null,
91+
binaryType: binaryType
8992
};
93+
9094
return id;
9195
},
9296

@@ -96,17 +100,18 @@ var WebSocketLibrary =
96100
* @param instanceId Instance ID
97101
* @param protocol Sub Protocol
98102
*/
99-
WebSocketAddSubProtocol: function(instanceId, protocol)
103+
WebSocketAddSubProtocol: function(instanceId, protocolPtr)
100104
{
101105
var instance = webSocketManager.instances[instanceId];
102106
if (!instance) return -1;
103107

104-
var protocolStr = UTF8ToString(protocol);
108+
var protocol = UTF8ToString(protocolPtr);
105109

106110
if(instance.subProtocols == null)
107111
instance.subProtocols = [];
108112

109-
instance.subProtocols.push(protocolStr);
113+
instance.subProtocols.push(protocol);
114+
110115
return 0;
111116
},
112117

@@ -149,25 +154,23 @@ var WebSocketLibrary =
149154
else
150155
instance.ws = new WebSocket(instance.url);
151156

157+
instance.ws.binaryType = instance.binaryType;
158+
152159
instance.ws.onopen = function()
153160
{
154-
if (webSocketManager.onOpen)
155-
Module.dynCall_vi(webSocketManager.onOpen, instanceId);
161+
Module.dynCall_vi(webSocketManager.onOpen, instanceId);
156162
};
157163

158164
instance.ws.onmessage = function(ev)
159165
{
160-
if (webSocketManager.onMessage === null)
161-
return;
162-
163166
if (ev.data instanceof ArrayBuffer)
164167
{
165-
var dataBuffer = new Uint8Array(ev.data);
166-
var buffer = _malloc(dataBuffer.length);
167-
HEAPU8.set(dataBuffer, buffer);
168+
var array = new Uint8Array(ev.data);
169+
var buffer = _malloc(array.length);
170+
writeArrayToMemory(array, buffer);
168171
try
169172
{
170-
Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, dataBuffer.length);
173+
Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, array.length);
171174
}
172175
finally
173176
{
@@ -177,21 +180,21 @@ var WebSocketLibrary =
177180
else if (ev.data instanceof Blob)
178181
{
179182
var reader = new FileReader();
180-
reader.addEventListener("loadend", function()
183+
reader.onload = function()
181184
{
182-
var dataBuffer = new Uint8Array(reader.result);
183-
var buffer = _malloc(dataBuffer.length);
184-
HEAPU8.set(dataBuffer, buffer);
185+
var array = new Uint8Array(reader.result);
186+
var buffer = _malloc(array.length);
187+
writeArrayToMemory(array, buffer);
185188
try
186189
{
187-
Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, dataBuffer.length);
190+
Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, array.length);
188191
}
189192
finally
190193
{
191194
reader = null;
192195
_free(buffer);
193196
}
194-
});
197+
};
195198
reader.readAsArrayBuffer(ev.data);
196199
}
197200
else if(typeof ev.data == 'string')
@@ -216,43 +219,37 @@ var WebSocketLibrary =
216219

217220
instance.ws.onerror = function(ev)
218221
{
219-
if (webSocketManager.onError)
222+
var msg = "WebSocket error.";
223+
var length = lengthBytesUTF8(msg) + 1;
224+
var buffer = _malloc(length);
225+
stringToUTF8(msg, buffer, length);
226+
try
220227
{
221-
var msg = "WebSocket error.";
222-
var length = lengthBytesUTF8(msg) + 1;
223-
var buffer = _malloc(length);
224-
stringToUTF8(msg, buffer, length);
225-
try
226-
{
227-
Module.dynCall_vii(webSocketManager.onError, instanceId, buffer);
228-
}
229-
finally
230-
{
231-
_free(buffer);
232-
}
228+
Module.dynCall_vii(webSocketManager.onError, instanceId, buffer);
229+
}
230+
finally
231+
{
232+
_free(buffer);
233233
}
234234
};
235235

236236
instance.ws.onclose = function(ev)
237237
{
238-
if (webSocketManager.onClose)
238+
var msg = ev.reason;
239+
var length = lengthBytesUTF8(msg) + 1;
240+
var buffer = _malloc(length);
241+
stringToUTF8(msg, buffer, length);
242+
try
239243
{
240-
var msg = ev.reason;
241-
var length = lengthBytesUTF8(msg) + 1;
242-
var buffer = _malloc(length);
243-
stringToUTF8(msg, buffer, length);
244-
try
245-
{
246-
Module.dynCall_viii(webSocketManager.onClose, instanceId, ev.code, buffer);
247-
}
248-
finally
249-
{
250-
_free(buffer);
251-
}
244+
Module.dynCall_viii(webSocketManager.onClose, instanceId, ev.code, buffer);
245+
}
246+
finally
247+
{
248+
_free(buffer);
252249
}
253-
254250
instance.ws = null;
255251
};
252+
256253
return 0;
257254
},
258255

@@ -280,6 +277,7 @@ var WebSocketLibrary =
280277
{
281278
return -7;
282279
}
280+
283281
return 0;
284282
},
285283

@@ -297,7 +295,7 @@ var WebSocketLibrary =
297295
if (instance.ws === null) return -3;
298296
if (instance.ws.readyState !== 1) return -6;
299297

300-
instance.ws.send(HEAPU8.slice(bufferPtr, bufferPtr + length));
298+
instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length));
301299

302300
return 0;
303301
},
@@ -329,7 +327,7 @@ var WebSocketLibrary =
329327
{
330328
var instance = webSocketManager.instances[instanceId];
331329
if (!instance) return -1;
332-
if (instance.ws === null) return 3;
330+
if (instance.ws === null) return 3; // socket null as closed
333331

334332
return instance.ws.readyState;
335333
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,24 @@ public interface IWebSocket
115115
/// It indicates the current state of the connection.
116116
/// </para>
117117
/// <para>
118-
/// The default value is <see cref="WebSocketState.Connecting"/>.
118+
/// The default value is <see cref="WebSocketState.Closed"/>.
119119
/// </para>
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+
123136
/// <summary>
124137
/// Occurs when the WebSocket connection has been established.
125138
/// </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.6.6";
10+
public const string VERSION = "2.7.0";
1111
}
1212
}

Assets/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocket.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !NET_LEGACY && (UNITY_EDITOR || !UNITY_WEBGL)
1+
#if !NET_LEGACY && (UNITY_EDITOR || !UNITY_WEBGL)
22
using System;
33
using System.Collections.Generic;
44
using System.Text;
@@ -37,6 +37,8 @@ public WebSocketState ReadyState
3737
}
3838
}
3939

40+
public string BinaryType { get; set; } = "arraybuffer";
41+
4042
public event EventHandler<OpenEventArgs> OnOpen;
4143
public event EventHandler<CloseEventArgs> OnClose;
4244
public event EventHandler<ErrorEventArgs> OnError;
@@ -278,7 +280,7 @@ private void HandleOpen()
278280

279281
private void HandleMessage(Opcode opcode, byte[] rawData)
280282
{
281-
Log($"OnMessage, type: {opcode}, size: {rawData.Length}");
283+
Log($"OnMessage, type: {opcode}, size: {rawData.Length}\n{BitConverter.ToString(rawData)}");
282284
#if !UNITY_WEB_SOCKET_ENABLE_ASYNC
283285
HandleEventSync(new MessageEventArgs(opcode, rawData));
284286
#else

Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocket.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class WebSocket : IWebSocket
88
public string Address { get; private set; }
99
public string[] SubProtocols { get; private set; }
1010
public WebSocketState ReadyState { get { return (WebSocketState)WebSocketManager.WebSocketGetState(instanceId); } }
11+
public string BinaryType { get; set; } = "arraybuffer";
1112

1213
public event EventHandler<OpenEventArgs> OnOpen;
1314
public event EventHandler<CloseEventArgs> OnClose;
@@ -38,15 +39,15 @@ public WebSocket(string address, string[] subProtocols)
3839

3940
internal void AllocateInstance()
4041
{
41-
instanceId = WebSocketManager.AllocateInstance(this.Address);
42+
instanceId = WebSocketManager.AllocateInstance(this.Address, this.BinaryType);
4243
Log($"Allocate socket with instanceId: {instanceId}");
4344
if (this.SubProtocols == null) return;
4445
foreach (var protocol in this.SubProtocols)
4546
{
4647
if (string.IsNullOrEmpty(protocol)) continue;
4748
Log($"Add Sub Protocol {protocol}, with instanceId: {instanceId}");
4849
int code = WebSocketManager.WebSocketAddSubProtocol(instanceId, protocol);
49-
if (code < 0)
50+
if (code < 0)
5051
{
5152
HandleOnError(GetErrorMessageFromCode(code));
5253
break;

Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocketManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ internal static class WebSocketManager
3232
public static extern int WebSocketSend(int instanceId, byte[] dataPtr, int dataLength);
3333

3434
[DllImport("__Internal")]
35-
public static extern int WebSocketSendStr(int instanceId, string dataPtr);
35+
public static extern int WebSocketSendStr(int instanceId, string data);
3636

3737
[DllImport("__Internal")]
3838
public static extern int WebSocketGetState(int instanceId);
3939

4040
/* WebSocket JSLIB callback setters and other functions */
4141
[DllImport("__Internal")]
42-
public static extern int WebSocketAllocate(string url);
42+
public static extern int WebSocketAllocate(string url, string binaryType);
4343

4444
[DllImport("__Internal")]
4545
public static extern int WebSocketAddSubProtocol(int instanceId, string protocol);
@@ -127,10 +127,10 @@ public static void DelegateOnCloseEvent(int instanceId, int closeCode, IntPtr re
127127
}
128128
}
129129

130-
internal static int AllocateInstance(string address)
130+
internal static int AllocateInstance(string address, string binaryType)
131131
{
132132
if (!isInitialized) Initialize();
133-
return WebSocketAllocate(address);
133+
return WebSocketAllocate(address, binaryType);
134134
}
135135

136136
internal static void Add(WebSocket socket)

0 commit comments

Comments
 (0)