Skip to content

Commit a42e600

Browse files
author
yinlong
committed
v2.4.0
- 修改 SendAsync 接口,移除完成回调 - 修改非WebGL平台下 WebSocket 连接支持在主线程上异步操作 - 整理项目路径,删除Tests文件夹,整合至Demo下。
1 parent 1d4b8fb commit a42e600

File tree

14 files changed

+157
-225
lines changed

14 files changed

+157
-225
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
socket.CloseAsync();
8080
```
8181

82-
- 详细使用方法可参考项目中的 [UnityWebSocketTest.cs](Tests/UnityWebSocketTest.cs) 示例代码。
82+
- 详细使用方法可参考项目中的 [UnityWebSocketDemo.cs](Samples~/Demo/UnityWebSocketDemo.cs) 示例代码。
8383

8484

8585
### **注意(Warning)**

README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
socket.CloseAsync();
7979
```
8080

81-
- more detail usage, see the [UnityWebSocketTest.cs](Tests/UnityWebSocketTest.cs) code in project。
81+
- more detail usage, see the [UnityWebSocketDemo.cs](Samples~/Demo/UnityWebSocketDemo.cs) code in project。
8282

8383

8484
### **Attention(Warning)**

Tests.meta renamed to Samples~/Demo.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using UnityEngine;
22
using UnityWebSocket;
33

4-
public class UnityWebSocketTest : MonoBehaviour
4+
public class UnityWebSocketDemo : MonoBehaviour
55
{
66
public string url = "ws://echo.websocket.org";
77
private IWebSocket socket;
@@ -59,25 +59,22 @@ private void OnGUI()
5959
{
6060
if (!string.IsNullOrEmpty(sendText))
6161
{
62-
socket.SendAsync(sendText, () =>
63-
{
64-
if (logMessage)
65-
AddLog(string.Format("Send: {0}\n", sendText));
66-
sendCount += 1;
67-
});
62+
socket.SendAsync(sendText);
63+
if (logMessage)
64+
AddLog(string.Format("Send: {0}\n", sendText));
65+
sendCount += 1;
6866
}
6967
}
7068
if (GUILayout.Button("Send Bytes"))
7169
{
7270
if (!string.IsNullOrEmpty(sendText))
7371
{
7472
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
75-
socket.SendAsync(bytes, () =>
76-
{
77-
if (logMessage)
78-
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
79-
sendCount += 1;
80-
});
73+
socket.SendAsync(bytes);
74+
75+
if (logMessage)
76+
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
77+
sendCount += 1;
8178
}
8279
}
8380
if (GUILayout.Button("Send x100"))
@@ -87,12 +84,11 @@ private void OnGUI()
8784
for (int i = 0; i < 100; i++)
8885
{
8986
var text = (i + 1).ToString() + ". " + sendText;
90-
socket.SendAsync(text, () =>
91-
{
92-
if (logMessage)
93-
AddLog(string.Format("Send: {0}\n", text));
94-
sendCount += 1;
95-
});
87+
socket.SendAsync(text);
88+
89+
if (logMessage)
90+
AddLog(string.Format("Send: {0}\n", text));
91+
sendCount += 1;
9692
}
9793
}
9894
}
@@ -104,12 +100,10 @@ private void OnGUI()
104100
{
105101
var text = (i + 1).ToString() + ". " + sendText;
106102
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
107-
socket.SendAsync(bytes, () =>
108-
{
109-
if (logMessage)
110-
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
111-
sendCount += 1;
112-
});
103+
socket.SendAsync(bytes);
104+
if (logMessage)
105+
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
106+
sendCount += 1;
113107
}
114108
}
115109
}
@@ -174,4 +168,11 @@ private void Socket_OnError(object sender, ErrorEventArgs e)
174168
AddLog(string.Format("Error: {0}\n", e.Message));
175169
}
176170

171+
private void OnApplicationQuit()
172+
{
173+
if (socket != null && socket.ReadyState != WebSocketState.Closed)
174+
{
175+
socket.CloseAsync();
176+
}
177+
}
177178
}

Scripts/Runtime/Core/IWebSocket.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace UnityWebSocket
66
/// <para>IWebSocket indicate a network connection.</para>
77
/// <para>It can be connecting, connected, closing or closed state. </para>
88
/// <para>You can send and receive messages by using it.</para>
9-
/// <para>Register onreceive callback for handling received messages.</para>
9+
/// <para>Register callbacks for handling messages.</para>
1010
/// <para> ----------------------------------------------------------- </para>
1111
/// <para>IWebSocket 表示一个网络连接,</para>
1212
/// <para>它可以是 connecting connected closing closed 状态,</para>
@@ -69,48 +69,30 @@ public interface IWebSocket
6969
/// <param name="data">
7070
/// An array of <see cref="byte"/> that represents the binary data to send.
7171
/// </param>
72-
/// <param name="completed">
73-
/// <para>
74-
/// An <c>Action</c> delegate or <see langword="null"/>
75-
/// if not needed.
76-
/// </para>
77-
/// <para>
78-
/// The delegate invokes the method called when the send is complete.
79-
/// </para>
80-
/// </param>
8172
/// <exception cref="InvalidOperationException">
8273
/// The current state of the connection is not Open.
8374
/// </exception>
8475
/// <exception cref="ArgumentNullException">
8576
/// <paramref name="data"/> is <see langword="null"/>.
8677
/// </exception>
87-
void SendAsync(byte[] data, Action completed = null);
78+
void SendAsync(byte[] data);
8879

8980
/// <summary>
9081
/// Sends the specified data using the WebSocket connection.
9182
/// </summary>
9283
/// <param name="text">
9384
/// A <see cref="string"/> that represents the text data to send.
9485
/// </param>
95-
/// <param name="completed">
96-
/// <para>
97-
/// An <c>Action</c> delegate or <see langword="null"/>
98-
/// if not needed.
99-
/// </para>
100-
/// <para>
101-
/// The delegate invokes the method called when the send is complete.
102-
/// </para>
103-
/// </param>
10486
/// <exception cref="InvalidOperationException">
10587
/// The current state of the connection is not Open.
10688
/// </exception>
10789
/// <exception cref="ArgumentNullException">
10890
/// <paramref name="text"/> is <see langword="null"/>.
10991
/// </exception>
11092
/// <exception cref="ArgumentException">
111-
/// <paramref name="text"/> could be UTF-8 encoded.
93+
/// <paramref name="text"/> could not be UTF-8 encoded.
11294
/// </exception>
113-
void SendAsync(string text, Action completed = null);
95+
void SendAsync(string text);
11496

11597
/// <summary>
11698
/// get the address which to connect.

Scripts/Runtime/Core/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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 = "799329256@qq.com";
99
public const string AUHTOR = "psygame";
10-
public const string VERSION = "2.3.3";
10+
public const string VERSION = "2.4.0";
1111
public const string PACKAGE_NAME = "com.psygame.unitywebsocket";
1212
public const string UPM_URL = "https://github.com/psygame/UnityWebSocket.git";
1313
}

0 commit comments

Comments
 (0)