Skip to content

Commit 8f62586

Browse files
committed
[修改]1. 修改宏定义的切换异常
1 parent 2070a05 commit 8f62586

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

Runtime/Implementation/NoWebGL/WebSocket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !NET_LEGACY && (UNITY_EDITOR || !UNITY_WEBGL)
1+
#if !UNITY_WEBGL
22
using System;
33
using System.Text;
44
using System.Threading;

Runtime/Implementation/NoWebGL/WebSocketManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !NET_LEGACY && (UNITY_EDITOR || !UNITY_WEBGL)
1+
#if !UNITY_WEBGL
22
using System.Collections.Generic;
33
using UnityEngine;
44

Runtime/Implementation/WebGL/WebSocket.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !UNITY_EDITOR && UNITY_WEBGL
1+
#if UNITY_WEBGL
22

33
using System;
44

@@ -50,12 +50,21 @@ public WebSocket(string address, string[] subProtocols)
5050

5151
internal void AllocateInstance()
5252
{
53+
Log($"AllocateInstance: {this.Address}");
5354
instanceId = WebSocketManager.AllocateInstance(this.Address);
5455
Log($"Allocate socket with instanceId: {instanceId}");
55-
if (this.SubProtocols == null) return;
56+
if (this.SubProtocols == null)
57+
{
58+
return;
59+
}
60+
5661
foreach (var protocol in this.SubProtocols)
5762
{
58-
if (string.IsNullOrEmpty(protocol)) continue;
63+
if (string.IsNullOrEmpty(protocol))
64+
{
65+
continue;
66+
}
67+
5968
Log($"Add Sub Protocol {protocol}, with instanceId: {instanceId}");
6069
int code = WebSocketManager.WebSocketAddSubProtocol(instanceId, protocol);
6170
if (code < 0)
@@ -84,21 +93,30 @@ public void CloseAsync()
8493
{
8594
Log($"Close with instanceId: {instanceId}");
8695
int code = WebSocketManager.WebSocketClose(instanceId, (int)CloseStatusCode.Normal, "Normal Closure");
87-
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
96+
if (code < 0)
97+
{
98+
HandleOnError(GetErrorMessageFromCode(code));
99+
}
88100
}
89101

90102
public void SendAsync(string text)
91103
{
92104
Log($"Send, type: {Opcode.Text}, size: {text.Length}");
93105
int code = WebSocketManager.WebSocketSendStr(instanceId, text);
94-
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
106+
if (code < 0)
107+
{
108+
HandleOnError(GetErrorMessageFromCode(code));
109+
}
95110
}
96111

97112
public void SendAsync(byte[] data)
98113
{
99114
Log($"Send, type: {Opcode.Binary}, size: {data.Length}");
100115
int code = WebSocketManager.WebSocketSend(instanceId, data, data.Length);
101-
if (code < 0) HandleOnError(GetErrorMessageFromCode(code));
116+
if (code < 0)
117+
{
118+
HandleOnError(GetErrorMessageFromCode(code));
119+
}
102120
}
103121

104122
internal void HandleOnOpen()

Runtime/Implementation/WebGL/WebSocketManager.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !UNITY_EDITOR && UNITY_WEBGL
1+
#if UNITY_WEBGL
22
using System;
33
using System.Collections.Generic;
44
using System.Runtime.InteropServices;
@@ -16,9 +16,13 @@ internal static class WebSocketManager
1616

1717
/* Delegates */
1818
public delegate void OnOpenCallback(int instanceId);
19+
1920
public delegate void OnMessageCallback(int instanceId, IntPtr msgPtr, int msgSize);
21+
2022
public delegate void OnMessageStrCallback(int instanceId, IntPtr msgStrPtr);
23+
2124
public delegate void OnErrorCallback(int instanceId, IntPtr errorPtr);
25+
2226
public delegate void OnCloseCallback(int instanceId, int closeCode, IntPtr reasonPtr);
2327

2428
/* WebSocket JSLIB functions */
@@ -61,7 +65,7 @@ internal static class WebSocketManager
6165

6266
[DllImport("__Internal")]
6367
public static extern void WebSocketSetOnClose(OnCloseCallback callback);
64-
68+
6569
[DllImport("__Internal")]
6670
public static extern void WebSocketSetSupport6000();
6771

@@ -135,7 +139,11 @@ public static void DelegateOnCloseEvent(int instanceId, int closeCode, IntPtr re
135139

136140
internal static int AllocateInstance(string address)
137141
{
138-
if (!isInitialized) Initialize();
142+
if (!isInitialized)
143+
{
144+
Initialize();
145+
}
146+
139147
return WebSocketAllocate(address);
140148
}
141149

0 commit comments

Comments
 (0)