Skip to content

Commit dd20ba7

Browse files
committed
fix some bugs and add readme
1 parent a5bfee7 commit dd20ba7

22 files changed

+705
-186
lines changed

README.md

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
[(English)](README_EN.md)
3+
24
## Demo 线上测试地址
35
- [http://39.105.150.229/UnityWebSocketDemo/](http://39.105.150.229/UnityWebSocketDemo/)
46

@@ -7,84 +9,91 @@
79
### 1. [最新版本下载](https://github.com/y85171642/UnityWebSocket/releases)
810

911
### 2. 使用方法:
10-
- 导入 UnityWebSocket.unitypackage
1112

12-
- 创建WebSocket实例
13+
14+
- 在 Unity 中导入 UnityWebSocket.unitypackage
15+
16+
需要 Scripting Runtime Version = .Net 4.x
17+
18+
需要 WebGL LinkerTarger = asm.js or Both
19+
20+
- 使用 WebSocket
1321

1422
```csharp
1523
// 命名空间
1624
using UnityWebSocket;
25+
using UnityWebSocket.Synchronized;
1726

1827
// 创建实例
19-
string address = "ws://127.0.0.1:8730/test";
20-
WebSocket scoket = new WebSocket(address);
28+
WebSocket scoket = new WebSocket();
2129

2230
// 注册回调
23-
scoket.onOpen += OnOpen;
24-
scoket.onClose += OnClose;
25-
scoket.onMessage += OnMessage;
26-
socket.onError += OnError;
31+
scoket.OnOpen += OnOpen;
32+
scoket.OnClose += OnClose;
33+
scoket.OnMessage += OnMessage;
34+
socket.OnError += OnError;
2735

2836
// 连接
29-
socket.Connect();
37+
string address = "ws://echo.websocket.org";
38+
socket.ConnectAsync(address);
3039

31-
// 发送数据
32-
socket.Send(str); // 发送类型String数据
33-
socket.Send(bytes); // 发送byte[]类型数据
40+
// 发送数据(两种发送方式)
41+
socket.SendAsync(str); // 发送类型 String 类型数据
42+
socket.SendAsync(bytes); // 发送 byte[] 类型数据
3443
3544
// 关闭连接
36-
socket.Close();
45+
socket.CloseAsync();
3746
```
3847

39-
- 详细使用方法可参考项目中的Example示例,或参考 [websocket-sharp](https://github.com/sta/websocket-sharp) 的使用方法
48+
- 详细使用方法可参考项目中的 [Example](UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Example/TestWebSocket.cs) 示例代码
4049

4150
### 3. 模块说明
4251
- WebSocket.jslib
4352
语法格式需要遵循 [asm.js](http://www.ruanyifeng.com/blog/2017/09/asmjs_emscripten.html)
4453

45-
路径:Plugins/WebSocketJS/WebSocketJS.jslib
46-
作用:Unity发布WebGL版本会将其加入到js运行库中。
54+
路径:Plugins/WebSocketJS/WebSocketJS.jslib
55+
作用:Unity发布WebGL版本会将其加入到js运行库中。
4756

4857
- WebSocket.cs
4958

50-
作用:WebSocket连接,可同时创建多个不同连接。
51-
已经支持全平台使用。
59+
作用:WebSocket连接,可同时创建多个不同连接。
60+
已经支持全平台使用。
5261

5362
- WebSocketReceiver.cs
5463

55-
作用:与jslib交互,负责收发多个WebSocket消息。
56-
该脚本在使用WebSocket时会自动加载到场景中,并添加到DonDestroyOnLoad。
64+
作用:与jslib交互,负责收发多个WebSocket消息。
65+
该脚本在使用WebSocket时会自动加载到场景中,并添加到DonDestroyOnLoad。
5766

5867
- Example场景
5968

60-
作用:WebSocket的使用方法示例。
69+
作用:WebSocket的使用方法示例。
6170

6271
### 4. 注意(Warning)
63-
- Unity2018 以上版本需要修改WebGL平台 Publishing Settings -> Linker Target 为 Both。
64-
- WebSocket的命名空间是 UnityWebSocket ,项目中有多个命名空间存在WebSocket类,不要用错了 :) 。
65-
- WebSocket的 onOpen、OnClose、OnMessage、OnError 回调都发生在网络线程中,回调处理函数不能直接修改主线程中的Unity组件内容,需要在主线程中加消息处理队列(需要加锁),缓存网络消息后,再在主线程中处理消息包。
66-
- WebGL平台下,暂时不能使用异步连接、关闭、发送,接口仍然使用的同步方式。
67-
- WebGL平台下,需要将打包好的文件,发布到Tomcat等服务器上运行。
68-
- v1.1 后版本加入了websocket-sharp插件(源码),如果你的项目已包含该插件,可选择较新版本。
72+
- Unity2018 以上版本需要修改WebGL平台 Publishing Settings -> Linker Target 为 asm.js 或 Both。
73+
- 插件中多个命名空间中存在 **WebSocket** 类,适用不同环境。
74+
75+
命名空间 | 平台 | 方式 | 说明
76+
-|-|-|-
77+
UnityWebSocket.Synchronized | 全平台 | 同步(无阻塞) | **[推荐]** 无需考虑异步回调使用 Unity 组件的问题。
78+
UnityWebSocket.Uniform | 全平台 | 异步 | 需要考虑异步回调使用 Unity 组件的问题。
79+
UnityWebSocket.WebGL | WebGL平台 | 异步 | 仅支持WebGL平台下的通信。
80+
UnityWebSocket.NoWebGL | 非WebGL平台 | 异步 | 仅支持非WebGL平台下的通信。
6981

7082
### 5. WebSocket服务器
7183
- 使用官方提供的 Echo Test 服务器。参考 [Echo Test](http://www.websocket.org/echo.html)
72-
- 需要自己搭建服务器,请参考 [websocket-sharp](https://github.com/sta/websocket-sharp) 的服务器示例。
73-
- WebSocket 测试地址:
74-
75-
wss://demos.kaazing.com/echo
76-
ws://demos.kaazing.com/echo
77-
ws://47.100.28.149:8758/test
78-
ws://47.100.28.149:8759/test
7984

8085
### 6. 版本记录
8186

87+
#### v2.0
88+
- 移除 websocket-sharp 插件,使用 .Net 4.x 内置的 ClientWebSocket 作为非 WebGL 平台下 WebSocket 插件。
89+
- 添加**同步方式**的WebSocket ,使用者不必再考虑**异步回调**中使用 Unity 组件的问题。
90+
8291
#### v1.3.2
83-
- 修复 非ssl连接,使用sslConfiguration bug。
92+
- 修复 非ssl连接,使用sslConfiguration bug。
8493

8594
#### v1.3.1
8695
- 修复 Tls error,添加默认协议 Tls,Tls11,Tls12。
87-
96+
8897
#### v1.3
8998
- 移除服务器Demo,改用 [websocket-sharp](http://www.websocket.org/echo.html) 官方提供的测试服务器。
9099
- 添加 PlayerSetting -> Linker Target 属性检测。

UnityWebSocket/Assembly-CSharp.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@
6969
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Core\IWebSocket.cs" />
7070
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Core\MessageEventArgs.cs" />
7171
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Core\Opcode.cs" />
72+
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Core\OpenEventArgs.cs" />
7273
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Core\WebSocketState.cs" />
7374
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Example\TestWebSocket.cs" />
74-
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\Common\WebSocket.cs" />
75+
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\NoWebGL\WebSocket.cs" />
76+
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\Synchronized\UnityWebSocketDebuger.cs" />
77+
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\Synchronized\UnityWebSocketManager.cs" />
78+
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\Synchronized\WebSocket.cs" />
7579
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\Uniform\WebSocket.cs" />
7680
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\WebGL\WebSocket.cs" />
7781
<Compile Include="Assets\Scripts\Plugins\UnityWebSocket\Implementation\WebGL\WebSocketReceiver.cs" />

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Core/CloseEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace UnityWebSocket
44
{
55
/// <summary>
6-
/// Represents the event data for the <see cref="WebSocket.OnClose"/> event.
6+
/// Represents the event data for the <see cref="IWebSocket.OnClose"/> event.
77
/// </summary>
88
/// <remarks>
99
/// <para>

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Core/ErrorEventArgs.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace UnityWebSocket
44
{
55
/// <summary>
6-
/// Represents the event data for the <see cref="WebSocket.OnError"/> event.
6+
/// Represents the event data for the <see cref="IWebSocket.OnError"/> event.
77
/// </summary>
88
/// <remarks>
99
/// <para>
10-
/// That event occurs when the <see cref="WebSocket"/> gets an error.
10+
/// That event occurs when the <see cref="IWebSocket"/> gets an error.
1111
/// </para>
1212
/// <para>
1313
/// If you would like to get the error message, you should access
14-
/// the <see cref="ErrorEventArgs.Message"/> property.
14+
/// the <see cref="Message"/> property.
1515
/// </para>
1616
/// <para>
1717
/// And if the error is due to an exception, you can get it by accessing
18-
/// the <see cref="ErrorEventArgs.Exception"/> property.
18+
/// the <see cref="Exception"/> property.
1919
/// </para>
2020
/// </remarks>
2121
public class ErrorEventArgs : EventArgs

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Core/IWebSocket.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
namespace UnityWebSocket
44
{
5+
/// <summary>
6+
/// <para>IWebSocket indicate a network connection.</para>
7+
/// <para>It can be connecting, connected, closing or closed state. </para>
8+
/// <para>You can send and receive messages by using it.</para>
9+
/// <para>Register receive callback for handling received messages.</para>
10+
/// <para>IWebSocket 表示一个网络连接,</para>
11+
/// <para>它可以是 connecting connected closing closed 状态,</para>
12+
/// <para>可以发送和接收消息,</para>
13+
/// <para>接收消息处理的地方注册消息回调即可。</para>
14+
/// </summary>
515
public interface IWebSocket
616
{
717
/// <summary>
@@ -60,41 +70,46 @@ public interface IWebSocket
6070
/// </param>
6171
/// <param name="completed">
6272
/// <para>
63-
/// An <c>Action&lt;bool&gt;</c> delegate or <see langword="null"/>
73+
/// An <c>Action</c> delegate or <see langword="null"/>
6474
/// if not needed.
6575
/// </para>
6676
/// <para>
6777
/// The delegate invokes the method called when the send is complete.
6878
/// </para>
69-
/// <para>
70-
/// <c>true</c> is passed to the method if the send has done with
71-
/// no error; otherwise, <c>false</c>.
72-
/// </para>
7379
/// </param>
7480
/// <exception cref="InvalidOperationException">
7581
/// The current state of the connection is not Open.
7682
/// </exception>
7783
/// <exception cref="ArgumentNullException">
7884
/// <paramref name="data"/> is <see langword="null"/>.
7985
/// </exception>
80-
void SendAsync(byte[] data, Action<bool> completed);
86+
void SendAsync(byte[] data, Action completed = null);
8187

8288
/// <summary>
8389
/// Sends the specified data using the WebSocket connection.
8490
/// </summary>
8591
/// <param name="text">
8692
/// A <see cref="string"/> that represents the text data to send.
8793
/// </param>
94+
/// <param name="completed">
95+
/// <para>
96+
/// An <c>Action</c> delegate or <see langword="null"/>
97+
/// if not needed.
98+
/// </para>
99+
/// <para>
100+
/// The delegate invokes the method called when the send is complete.
101+
/// </para>
102+
/// </param>
88103
/// <exception cref="InvalidOperationException">
89104
/// The current state of the connection is not Open.
90105
/// </exception>
91106
/// <exception cref="ArgumentNullException">
92107
/// <paramref name="text"/> is <see langword="null"/>.
93108
/// </exception>
94109
/// <exception cref="ArgumentException">
95-
/// <paramref name="text"/> could not be UTF-8-encoded.
110+
/// <paramref name="text"/> could be UTF-8 encoded.
96111
/// </exception>
97-
void SendAsync(string text, Action<bool> completed);
112+
void SendAsync(string text, Action completed = null);
98113

99114
/// <summary>
100115
/// get the address which to connect.
@@ -120,20 +135,20 @@ public interface IWebSocket
120135
/// <summary>
121136
/// Occurs when the WebSocket connection has been established.
122137
/// </summary>
123-
event EventHandler OnOpen;
138+
event EventHandler<OpenEventArgs> OnOpen;
124139

125140
/// <summary>
126141
/// Occurs when the WebSocket connection has been closed.
127142
/// </summary>
128143
event EventHandler<CloseEventArgs> OnClose;
129144

130145
/// <summary>
131-
/// Occurs when the <see cref="WebSocket"/> gets an error.
146+
/// Occurs when the <see cref="IWebSocket"/> gets an error.
132147
/// </summary>
133148
event EventHandler<ErrorEventArgs> OnError;
134149

135150
/// <summary>
136-
/// Occurs when the <see cref="WebSocket"/> receives a message.
151+
/// Occurs when the <see cref="IWebSocket"/> receives a message.
137152
/// </summary>
138153
event EventHandler<MessageEventArgs> OnMessage;
139154
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace UnityWebSocket
4+
{
5+
public class OpenEventArgs : EventArgs
6+
{
7+
internal OpenEventArgs()
8+
{
9+
}
10+
}
11+
}

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Core/OpenEventArgs.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Editor/WebSocketPlayerSettingCheck.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static void CheckSettings()
4343
else if (PlayerSettings.scriptingRuntimeVersion == ScriptingRuntimeVersion.Legacy)
4444
{
4545
EditorUtility.DisplayDialog("Warning"
46-
, "Scripting Runtime Version should be .NET 4.x, via Menu:\nPlayerSettings -> Other Settings -> Script Runtime Version -> .Net 4.x Equivalent"
46+
, "Scripting Runtime Version should be .NET 4.x, via Menu:\nPlayerSettings -> Other Settings -> Scripting Runtime Version -> .Net 4.x Equivalent"
4747
, "OK");
4848
}
4949
else
@@ -67,7 +67,7 @@ public static void OnInit()
6767
if (PlayerSettings.scriptingRuntimeVersion == ScriptingRuntimeVersion.Legacy)
6868
{
6969
EditorUtility.DisplayDialog("Warning"
70-
, "Scripting Runtime Version should be .NET 4.x, via Menu:\nPlayerSettings -> Other Settings -> Script Runtime Version -> .Net 4.x Equivalent"
70+
, "Scripting Runtime Version should be .NET 4.x, via Menu:\nPlayerSettings -> Other Settings -> Scripting Runtime Version -> .Net 4.x Equivalent"
7171
, "OK");
7272
}
7373
}

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Example/TestWebSocket.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using UnityEngine;
22
using UnityWebSocket;
3+
using UnityWebSocket.Synchronized;
34

45
public class TestWebSocket : MonoBehaviour
56
{
67
public string url = "ws://echo.websocket.org";
7-
WebSocket socket;
8+
private WebSocket socket;
9+
810
private void Awake()
911
{
1012
socket = new WebSocket();
@@ -14,7 +16,6 @@ private void Awake()
1416
socket.OnError += Socket_OnError;
1517
}
1618

17-
1819
private void Socket_OnOpen(object sender, System.EventArgs e)
1920
{
2021
message += string.Format("Connected: {0}\n", url);
@@ -70,7 +71,13 @@ private void OnGUI()
7071

7172
if (GUILayout.Button("Send"))
7273
{
73-
socket.SendAsync(sendText, null);
74+
if (!string.IsNullOrEmpty(sendText))
75+
{
76+
socket.SendAsync(sendText, () =>
77+
{
78+
message += string.Format("Send: {0}\n", sendText);
79+
});
80+
}
7481
}
7582

7683
GUI.enabled = true;
@@ -84,6 +91,5 @@ private void OnGUI()
8491
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 250), width);
8592
GUILayout.Label(message);
8693
GUILayout.EndScrollView();
87-
8894
}
89-
}
95+
}

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Implementation/Common.meta renamed to UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Implementation/NoWebGL.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)