Skip to content

Commit

Permalink
v2.4.0
Browse files Browse the repository at this point in the history
- 修改 SendAsync 接口,移除完成回调
- 修改非WebGL平台下 WebSocket 连接支持在主线程上异步操作
- 整理项目路径,删除Tests文件夹,整合至Demo下。
  • Loading branch information
yinlong committed Sep 18, 2020
1 parent 1d4b8fb commit a42e600
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 225 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
socket.CloseAsync();
```

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


### **注意(Warning)**
Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
socket.CloseAsync();
```

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


### **Attention(Warning)**
Expand Down
4 changes: 1 addition & 3 deletions Tests.meta → Samples~/Demo.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 26 additions & 25 deletions Tests/UnityWebSocketTest.cs → Samples~/Demo/UnityWebSocketDemo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;
using UnityWebSocket;

public class UnityWebSocketTest : MonoBehaviour
public class UnityWebSocketDemo : MonoBehaviour
{
public string url = "ws://echo.websocket.org";
private IWebSocket socket;
Expand Down Expand Up @@ -59,25 +59,22 @@ private void OnGUI()
{
if (!string.IsNullOrEmpty(sendText))
{
socket.SendAsync(sendText, () =>
{
if (logMessage)
AddLog(string.Format("Send: {0}\n", sendText));
sendCount += 1;
});
socket.SendAsync(sendText);
if (logMessage)
AddLog(string.Format("Send: {0}\n", sendText));
sendCount += 1;
}
}
if (GUILayout.Button("Send Bytes"))
{
if (!string.IsNullOrEmpty(sendText))
{
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
socket.SendAsync(bytes, () =>
{
if (logMessage)
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
sendCount += 1;
});
socket.SendAsync(bytes);

if (logMessage)
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
sendCount += 1;
}
}
if (GUILayout.Button("Send x100"))
Expand All @@ -87,12 +84,11 @@ private void OnGUI()
for (int i = 0; i < 100; i++)
{
var text = (i + 1).ToString() + ". " + sendText;
socket.SendAsync(text, () =>
{
if (logMessage)
AddLog(string.Format("Send: {0}\n", text));
sendCount += 1;
});
socket.SendAsync(text);

if (logMessage)
AddLog(string.Format("Send: {0}\n", text));
sendCount += 1;
}
}
}
Expand All @@ -104,12 +100,10 @@ private void OnGUI()
{
var text = (i + 1).ToString() + ". " + sendText;
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
socket.SendAsync(bytes, () =>
{
if (logMessage)
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
sendCount += 1;
});
socket.SendAsync(bytes);
if (logMessage)
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
sendCount += 1;
}
}
}
Expand Down Expand Up @@ -174,4 +168,11 @@ private void Socket_OnError(object sender, ErrorEventArgs e)
AddLog(string.Format("Error: {0}\n", e.Message));
}

private void OnApplicationQuit()
{
if (socket != null && socket.ReadyState != WebSocketState.Closed)
{
socket.CloseAsync();
}
}
}
File renamed without changes.
26 changes: 4 additions & 22 deletions Scripts/Runtime/Core/IWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UnityWebSocket
/// <para>IWebSocket indicate a network connection.</para>
/// <para>It can be connecting, connected, closing or closed state. </para>
/// <para>You can send and receive messages by using it.</para>
/// <para>Register onreceive callback for handling received messages.</para>
/// <para>Register callbacks for handling messages.</para>
/// <para> ----------------------------------------------------------- </para>
/// <para>IWebSocket 表示一个网络连接,</para>
/// <para>它可以是 connecting connected closing closed 状态,</para>
Expand Down Expand Up @@ -69,48 +69,30 @@ public interface IWebSocket
/// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send.
/// </param>
/// <param name="completed">
/// <para>
/// An <c>Action</c> delegate or <see langword="null"/>
/// if not needed.
/// </para>
/// <para>
/// The delegate invokes the method called when the send is complete.
/// </para>
/// </param>
/// <exception cref="InvalidOperationException">
/// The current state of the connection is not Open.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="data"/> is <see langword="null"/>.
/// </exception>
void SendAsync(byte[] data, Action completed = null);
void SendAsync(byte[] data);

/// <summary>
/// Sends the specified data using the WebSocket connection.
/// </summary>
/// <param name="text">
/// A <see cref="string"/> that represents the text data to send.
/// </param>
/// <param name="completed">
/// <para>
/// An <c>Action</c> delegate or <see langword="null"/>
/// if not needed.
/// </para>
/// <para>
/// The delegate invokes the method called when the send is complete.
/// </para>
/// </param>
/// <exception cref="InvalidOperationException">
/// The current state of the connection is not Open.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="text"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="text"/> could be UTF-8 encoded.
/// <paramref name="text"/> could not be UTF-8 encoded.
/// </exception>
void SendAsync(string text, Action completed = null);
void SendAsync(string text);

/// <summary>
/// get the address which to connect.
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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 = "psygame";
public const string VERSION = "2.3.3";
public const string VERSION = "2.4.0";
public const string PACKAGE_NAME = "com.psygame.unitywebsocket";
public const string UPM_URL = "https://github.com/psygame/UnityWebSocket.git";
}
Expand Down
Loading

0 comments on commit a42e600

Please sign in to comment.