Skip to content

Commit

Permalink
v2.8.0 性能优化及Bug修,优化部分功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
psygames committed Aug 24, 2024
2 parents 674f6c7 + 99da436 commit c57bed0
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 187 deletions.
11 changes: 10 additions & 1 deletion Assets/UnityWebSocket/Demo/UnityWebSocketDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ private void OnGUI()

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

// draw header
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();

// draw websocket state
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();

// draw address
GUI.enabled = state == WebSocketState.Closed;
GUILayout.Label("Address: ", width);
address = GUILayout.TextField(address, width);

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

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

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

// draw send message button
GUILayout.BeginHorizontal();
if (GUILayout.Button("Send") && !string.IsNullOrEmpty(sendText))
{
Expand Down Expand Up @@ -103,23 +110,25 @@ private void OnGUI()
sendCount += 1;
}
}

GUILayout.EndHorizontal();

// draw message count
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();

// draw clear button
if (GUILayout.Button("Clear"))
{
log = "";
receiveCount = 0;
sendCount = 0;
}

// draw message content
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 270), width);
GUILayout.Label(log);
GUILayout.EndScrollView();
Expand Down
53 changes: 27 additions & 26 deletions Assets/UnityWebSocket/Plugins/WebGL/WebSocket.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var WebSocketLibrary =
* {
* url: string,
* ws: WebSocket,
* binaryType: string,
* subProtocols: string[],
* }
*/
Expand All @@ -21,6 +20,7 @@ var WebSocketLibrary =
/* Event listeners */
onOpen: null,
onMessage: null,
onMessageStr: null,
onError: null,
onClose: null
},
Expand All @@ -46,7 +46,7 @@ var WebSocketLibrary =
},

/**
* Set onMessage callback
* Set onMessageStr callback
*
* @param callback Reference to C# static function
*/
Expand Down Expand Up @@ -80,15 +80,13 @@ var WebSocketLibrary =
*
* @param url Server URL
*/
WebSocketAllocate: function(urlPtr, binaryTypePtr)
WebSocketAllocate: function(urlPtr)
{
var url = UTF8ToString(urlPtr);
var binaryType = UTF8ToString(binaryTypePtr);
var id = ++webSocketManager.lastId;
webSocketManager.instances[id] = {
url: url,
ws: null,
binaryType: binaryType
};

return id;
Expand All @@ -107,7 +105,7 @@ var WebSocketLibrary =

var protocol = UTF8ToString(protocolPtr);

if(instance.subProtocols == null)
if (instance.subProtocols == null)
instance.subProtocols = [];

instance.subProtocols.push(protocol);
Expand Down Expand Up @@ -149,13 +147,11 @@ var WebSocketLibrary =
if (!instance) return -1;
if (instance.ws !== null) return -2;

if(instance.subProtocols != null)
if (instance.subProtocols != null)
instance.ws = new WebSocket(instance.url, instance.subProtocols);
else
instance.ws = new WebSocket(instance.url);

instance.ws.binaryType = instance.binaryType;

instance.ws.onopen = function()
{
Module.dynCall_vi(webSocketManager.onOpen, instanceId);
Expand All @@ -177,7 +173,21 @@ var WebSocketLibrary =
_free(buffer);
}
}
else if (ev.data instanceof Blob)
else if (typeof ev.data == 'string')
{
var length = lengthBytesUTF8(ev.data) + 1;
var buffer = _malloc(length);
stringToUTF8(ev.data, buffer, length);
try
{
Module.dynCall_vii(webSocketManager.onMessageStr, instanceId, buffer);
}
finally
{
_free(buffer);
}
}
else if (typeof Blob !== 'undefined' && ev.data instanceof Blob)
{
var reader = new FileReader();
reader.onload = function()
Expand All @@ -197,20 +207,6 @@ var WebSocketLibrary =
};
reader.readAsArrayBuffer(ev.data);
}
else if(typeof ev.data == 'string')
{
var length = lengthBytesUTF8(ev.data) + 1;
var buffer = _malloc(length);
stringToUTF8(ev.data, buffer, length);
try
{
Module.dynCall_vii(webSocketManager.onMessageStr, instanceId, buffer);
}
finally
{
_free(buffer);
}
}
else
{
console.log("[JSLIB WebSocket] not support message type: ", (typeof ev.data));
Expand Down Expand Up @@ -273,7 +269,7 @@ var WebSocketLibrary =
{
instance.ws.close(code, reason);
}
catch(err)
catch (err)
{
return -7;
}
Expand All @@ -295,7 +291,12 @@ var WebSocketLibrary =
if (instance.ws === null) return -3;
if (instance.ws.readyState !== 1) return -6;

instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length));
if (typeof HEAPU8 !== 'undefined')
instance.ws.send(HEAPU8.buffer.slice(bufferPtr, bufferPtr + length));
else if (typeof buffer !== 'undefined')
instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length));
else
return -8; // not support buffer slice

return 0;
},
Expand Down
19 changes: 1 addition & 18 deletions Assets/UnityWebSocket/Scripts/Editor/SettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,12 @@ private void DrawVersion()
{
if (GUI.Button(new Rect(440, 50, 150, 18), "Update to | " + latestVersion))
{
ShowUpdateDialog();
Application.OpenURL(Settings.GITHUB + "/releases");
}
}
}
}

private void ShowUpdateDialog()
{
var isOK = EditorUtility.DisplayDialog("UnityWebSocket",
"Update UnityWebSocket now?\n" + changeLog,
"Update Now", "Cancel");

if (isOK)
{
UpdateVersion();
}
}

private void UpdateVersion()
{
Application.OpenURL(Settings.GITHUB + "/releases");
}

private void DrawHelper()
{
GUI.Label(new Rect(330, 200, 100, 18), "GitHub:", TextStyle(10, TextAnchor.MiddleRight));
Expand Down
15 changes: 1 addition & 14 deletions Assets/UnityWebSocket/Scripts/Runtime/Core/IWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace UnityWebSocket
{
Expand Down Expand Up @@ -120,19 +120,6 @@ public interface IWebSocket
/// </value>
WebSocketState ReadyState { get; }

/// <summary>
/// Gets the current binaryType of the connection, supported on WEBGL platform only.
/// </summary>
/// <value>
/// <para>
/// It indicates the current binaryType of the connection.
/// </para>
/// <para>
/// The default value is "arraybuffer", options: "blob" or "arraybuffer".
/// </para>
/// </value>
string BinaryType { get; set; }

/// <summary>
/// Occurs when the WebSocket connection has been established.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Assets/UnityWebSocket/Scripts/Runtime/Core/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace UnityWebSocket
namespace UnityWebSocket
{
public static class Settings
{
Expand All @@ -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.7.0";
public const string VERSION = "2.8.0";
}
}
Loading

0 comments on commit c57bed0

Please sign in to comment.