Skip to content

Commit

Permalink
修复Unity运行时,触发代码编译导致卡死问题。性能优化,修改Socket发送线程休眠时间。
Browse files Browse the repository at this point in the history
  • Loading branch information
psygames committed Nov 8, 2024
1 parent 6b0b1ab commit a4a6a89
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Assets/UnityWebSocket/Scripts/Editor/SettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void DrawHelper()
Application.OpenURL(uri.AbsoluteUri);
}

GUI.Label(new Rect(330, 275, 100, 18), "QQ群:", TextStyle(10, TextAnchor.MiddleRight));
GUI.Label(new Rect(330, 275, 100, 18), "QQ Group:", TextStyle(10, TextAnchor.MiddleRight));
if (GUI.Button(new Rect(440, 275, 150, 18), Settings.QQ_GROUP))
{
Application.OpenURL(Settings.QQ_GROUP_LINK);
Expand Down
2 changes: 1 addition & 1 deletion Assets/UnityWebSocket/Scripts/Runtime/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.8.3";
public const string VERSION = "2.8.4";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private async void StartSendTask()
Log($"Send, type: {buffer.type}, size: {buffer.data.Length}, queue left: {sendQueue.Count}");
await socket.SendAsync(new ArraySegment<byte>(buffer.data), buffer.type, true, cts.Token);
}
Thread.Sleep(1);
Thread.Sleep(3);
}
if (closeProcessing && socket != null && cts != null && !cts.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace UnityWebSocket
{
[DisallowMultipleComponent]
[DefaultExecutionOrder(-10000)]
internal class WebSocketManager : MonoBehaviour
{
Expand All @@ -29,6 +30,10 @@ public static void CreateInstance()
if (!go) go = new GameObject(rootName);
_instance = go.GetComponent<WebSocketManager>();
if (!_instance) _instance = go.AddComponent<WebSocketManager>();
#if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
UnityEditor.Compilation.CompilationPipeline.compilationStarted -= OnCompilationStarted;
UnityEditor.Compilation.CompilationPipeline.compilationStarted += OnCompilationStarted;
#endif
}

private readonly List<WebSocket> sockets = new List<WebSocket>();
Expand All @@ -55,7 +60,22 @@ private void Update()
}

#if UNITY_EDITOR
#if UNITY_2019_1_OR_NEWER
private static void OnCompilationStarted(object obj)
{
if (_instance != null)
{
_instance.SocketAbort();
}
}
#endif

private void OnApplicationQuit()
{
SocketAbort();
}

private void SocketAbort()
{
for (int i = sockets.Count - 1; i >= 0; i--)
{
Expand Down

0 comments on commit a4a6a89

Please sign in to comment.