From ce7cf5b35ad2504a30f7aa78da6f6878f31a9a97 Mon Sep 17 00:00:00 2001 From: psygames

Date: Wed, 19 Oct 2022 10:09:03 +0800 Subject: [PATCH] websocket js support for wegame & code optimize --- .../Plugins/WebGL/WebSocket.jslib | 102 +++++++++--------- .../Scripts/Runtime/Core/IWebSocket.cs | 15 ++- .../Scripts/Runtime/Core/Settings.cs | 4 +- .../Implementation/NoWebGL/WebSocket.cs | 6 +- .../Runtime/Implementation/WebGL/WebSocket.cs | 5 +- .../Implementation/WebGL/WebSocketManager.cs | 8 +- 6 files changed, 77 insertions(+), 63 deletions(-) diff --git a/Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib b/Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib index 68fdeb2..a5ba581 100644 --- a/Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib +++ b/Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib @@ -9,7 +9,8 @@ var WebSocketLibrary = * { * url: string, * ws: WebSocket, - * subProtocols: string[], + * binaryType: string, + * subProtocols: string[], * } */ instances: {}, @@ -79,14 +80,17 @@ var WebSocketLibrary = * * @param url Server URL */ - WebSocketAllocate: function(url) + WebSocketAllocate: function(urlPtr, binaryTypePtr) { - var urlStr = UTF8ToString(url); + var url = UTF8ToString(urlPtr); + var binaryType = UTF8ToString(binaryTypePtr); var id = ++webSocketManager.lastId; webSocketManager.instances[id] = { - url: urlStr, - ws: null + url: url, + ws: null, + binaryType: binaryType }; + return id; }, @@ -96,17 +100,18 @@ var WebSocketLibrary = * @param instanceId Instance ID * @param protocol Sub Protocol */ - WebSocketAddSubProtocol: function(instanceId, protocol) + WebSocketAddSubProtocol: function(instanceId, protocolPtr) { var instance = webSocketManager.instances[instanceId]; if (!instance) return -1; - var protocolStr = UTF8ToString(protocol); + var protocol = UTF8ToString(protocolPtr); if(instance.subProtocols == null) instance.subProtocols = []; - instance.subProtocols.push(protocolStr); + instance.subProtocols.push(protocol); + return 0; }, @@ -149,25 +154,23 @@ var WebSocketLibrary = else instance.ws = new WebSocket(instance.url); + instance.ws.binaryType = instance.binaryType; + instance.ws.onopen = function() { - if (webSocketManager.onOpen) - Module.dynCall_vi(webSocketManager.onOpen, instanceId); + Module.dynCall_vi(webSocketManager.onOpen, instanceId); }; instance.ws.onmessage = function(ev) { - if (webSocketManager.onMessage === null) - return; - if (ev.data instanceof ArrayBuffer) { - var dataBuffer = new Uint8Array(ev.data); - var buffer = _malloc(dataBuffer.length); - HEAPU8.set(dataBuffer, buffer); + var array = new Uint8Array(ev.data); + var buffer = _malloc(array.length); + writeArrayToMemory(array, buffer); try { - Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, dataBuffer.length); + Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, array.length); } finally { @@ -177,21 +180,21 @@ var WebSocketLibrary = else if (ev.data instanceof Blob) { var reader = new FileReader(); - reader.addEventListener("loadend", function() + reader.onload = function() { - var dataBuffer = new Uint8Array(reader.result); - var buffer = _malloc(dataBuffer.length); - HEAPU8.set(dataBuffer, buffer); + var array = new Uint8Array(reader.result); + var buffer = _malloc(array.length); + writeArrayToMemory(array, buffer); try { - Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, dataBuffer.length); + Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, array.length); } finally { reader = null; _free(buffer); } - }); + }; reader.readAsArrayBuffer(ev.data); } else if(typeof ev.data == 'string') @@ -216,43 +219,37 @@ var WebSocketLibrary = instance.ws.onerror = function(ev) { - if (webSocketManager.onError) + var msg = "WebSocket error."; + var length = lengthBytesUTF8(msg) + 1; + var buffer = _malloc(length); + stringToUTF8(msg, buffer, length); + try { - var msg = "WebSocket error."; - var length = lengthBytesUTF8(msg) + 1; - var buffer = _malloc(length); - stringToUTF8(msg, buffer, length); - try - { - Module.dynCall_vii(webSocketManager.onError, instanceId, buffer); - } - finally - { - _free(buffer); - } + Module.dynCall_vii(webSocketManager.onError, instanceId, buffer); + } + finally + { + _free(buffer); } }; instance.ws.onclose = function(ev) { - if (webSocketManager.onClose) + var msg = ev.reason; + var length = lengthBytesUTF8(msg) + 1; + var buffer = _malloc(length); + stringToUTF8(msg, buffer, length); + try { - var msg = ev.reason; - var length = lengthBytesUTF8(msg) + 1; - var buffer = _malloc(length); - stringToUTF8(msg, buffer, length); - try - { - Module.dynCall_viii(webSocketManager.onClose, instanceId, ev.code, buffer); - } - finally - { - _free(buffer); - } + Module.dynCall_viii(webSocketManager.onClose, instanceId, ev.code, buffer); + } + finally + { + _free(buffer); } - instance.ws = null; }; + return 0; }, @@ -280,6 +277,7 @@ var WebSocketLibrary = { return -7; } + return 0; }, @@ -297,7 +295,7 @@ var WebSocketLibrary = if (instance.ws === null) return -3; if (instance.ws.readyState !== 1) return -6; - instance.ws.send(HEAPU8.slice(bufferPtr, bufferPtr + length)); + instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length)); return 0; }, @@ -329,7 +327,7 @@ var WebSocketLibrary = { var instance = webSocketManager.instances[instanceId]; if (!instance) return -1; - if (instance.ws === null) return 3; + if (instance.ws === null) return 3; // socket null as closed return instance.ws.readyState; } diff --git a/Assets/UnityWebSocket/Scripts/Runtime/Core/IWebSocket.cs b/Assets/UnityWebSocket/Scripts/Runtime/Core/IWebSocket.cs index a438812..514ec57 100644 --- a/Assets/UnityWebSocket/Scripts/Runtime/Core/IWebSocket.cs +++ b/Assets/UnityWebSocket/Scripts/Runtime/Core/IWebSocket.cs @@ -115,11 +115,24 @@ public interface IWebSocket /// It indicates the current state of the connection. /// /// - /// The default value is . + /// The default value is . /// /// WebSocketState ReadyState { get; } + ///

+ /// Gets the current binaryType of the connection, supported on WEBGL platform only. + /// + /// + /// + /// It indicates the current binaryType of the connection. + /// + /// + /// The default value is "arraybuffer", options: "blob" or "arraybuffer". + /// + /// + string BinaryType { get; set; } + /// /// Occurs when the WebSocket connection has been established. /// diff --git a/Assets/UnityWebSocket/Scripts/Runtime/Core/Settings.cs b/Assets/UnityWebSocket/Scripts/Runtime/Core/Settings.cs index 8829c52..04dd3ed 100644 --- a/Assets/UnityWebSocket/Scripts/Runtime/Core/Settings.cs +++ b/Assets/UnityWebSocket/Scripts/Runtime/Core/Settings.cs @@ -1,4 +1,4 @@ -namespace UnityWebSocket +namespace UnityWebSocket { public static class Settings { @@ -7,6 +7,6 @@ public static class Settings public const string QQ_GROUP_LINK = "https://qm.qq.com/cgi-bin/qm/qr?k=KcexYJ9aYwogFXbj2aN0XHH5b2G7ICmd"; public const string EMAIL = "799329256@qq.com"; public const string AUHTOR = "psygames"; - public const string VERSION = "2.6.6"; + public const string VERSION = "2.7.0"; } } diff --git a/Assets/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocket.cs b/Assets/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocket.cs index 6c7e296..6c4b2f4 100644 --- a/Assets/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocket.cs +++ b/Assets/UnityWebSocket/Scripts/Runtime/Implementation/NoWebGL/WebSocket.cs @@ -1,4 +1,4 @@ -#if !NET_LEGACY && (UNITY_EDITOR || !UNITY_WEBGL) +#if !NET_LEGACY && (UNITY_EDITOR || !UNITY_WEBGL) using System; using System.Collections.Generic; using System.Text; @@ -37,6 +37,8 @@ public WebSocketState ReadyState } } + public string BinaryType { get; set; } = "arraybuffer"; + public event EventHandler OnOpen; public event EventHandler OnClose; public event EventHandler OnError; @@ -278,7 +280,7 @@ private void HandleOpen() private void HandleMessage(Opcode opcode, byte[] rawData) { - Log($"OnMessage, type: {opcode}, size: {rawData.Length}"); + Log($"OnMessage, type: {opcode}, size: {rawData.Length}\n{BitConverter.ToString(rawData)}"); #if !UNITY_WEB_SOCKET_ENABLE_ASYNC HandleEventSync(new MessageEventArgs(opcode, rawData)); #else diff --git a/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocket.cs b/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocket.cs index 7af0f5a..95d048d 100644 --- a/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocket.cs +++ b/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocket.cs @@ -8,6 +8,7 @@ public class WebSocket : IWebSocket public string Address { get; private set; } public string[] SubProtocols { get; private set; } public WebSocketState ReadyState { get { return (WebSocketState)WebSocketManager.WebSocketGetState(instanceId); } } + public string BinaryType { get; set; } = "arraybuffer"; public event EventHandler OnOpen; public event EventHandler OnClose; @@ -38,7 +39,7 @@ public WebSocket(string address, string[] subProtocols) internal void AllocateInstance() { - instanceId = WebSocketManager.AllocateInstance(this.Address); + instanceId = WebSocketManager.AllocateInstance(this.Address, this.BinaryType); Log($"Allocate socket with instanceId: {instanceId}"); if (this.SubProtocols == null) return; foreach (var protocol in this.SubProtocols) @@ -46,7 +47,7 @@ internal void AllocateInstance() if (string.IsNullOrEmpty(protocol)) continue; Log($"Add Sub Protocol {protocol}, with instanceId: {instanceId}"); int code = WebSocketManager.WebSocketAddSubProtocol(instanceId, protocol); - if (code < 0) + if (code < 0) { HandleOnError(GetErrorMessageFromCode(code)); break; diff --git a/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocketManager.cs b/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocketManager.cs index f49b151..357d95e 100644 --- a/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocketManager.cs +++ b/Assets/UnityWebSocket/Scripts/Runtime/Implementation/WebGL/WebSocketManager.cs @@ -32,14 +32,14 @@ internal static class WebSocketManager public static extern int WebSocketSend(int instanceId, byte[] dataPtr, int dataLength); [DllImport("__Internal")] - public static extern int WebSocketSendStr(int instanceId, string dataPtr); + public static extern int WebSocketSendStr(int instanceId, string data); [DllImport("__Internal")] public static extern int WebSocketGetState(int instanceId); /* WebSocket JSLIB callback setters and other functions */ [DllImport("__Internal")] - public static extern int WebSocketAllocate(string url); + public static extern int WebSocketAllocate(string url, string binaryType); [DllImport("__Internal")] public static extern int WebSocketAddSubProtocol(int instanceId, string protocol); @@ -127,10 +127,10 @@ public static void DelegateOnCloseEvent(int instanceId, int closeCode, IntPtr re } } - internal static int AllocateInstance(string address) + internal static int AllocateInstance(string address, string binaryType) { if (!isInitialized) Initialize(); - return WebSocketAllocate(address); + return WebSocketAllocate(address, binaryType); } internal static void Add(WebSocket socket)