Skip to content

Commit 7e263db

Browse files
author
yinlong
committed
完善代码注释,完善变量名,修改帮助文档Readme
1 parent 86598bd commit 7e263db

File tree

5 files changed

+65
-69
lines changed

5 files changed

+65
-69
lines changed

README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,41 @@
44

55
# UnityWebSocket 使用
66

7-
### 1. [最新版本下载](https://github.com/y85171642/UnityWebSocket/releases/latest)
7+
### 1. [最新版本下载](https://github.com/y85171642/UnityWebSocket/releases)
88

99
### 2. 使用方法:
1010
- 导入 UnityWebSocket.unitypackage
1111

1212
- 创建WebSocket实例
1313

1414
```csharp
15+
// 命名空间
16+
using UnityWebSocket;
17+
1518
// 创建实例
1619
string address = "ws://127.0.0.1:8730/test";
1720
WebSocket scoket = new WebSocket(address);
1821

1922
// 注册回调
2023
scoket.onOpen += OnOpen;
2124
scoket.onClose += OnClose;
22-
scoket.onReceive += OnReceive;
25+
scoket.onMessage += OnMessage;
26+
socket.onError += OnError;
2327

2428
// 连接
2529
socket.Connect();
2630

2731
// 发送数据
28-
socket.Send(data);//发送数据类型byte[]
32+
socket.Send(str); // 发送类型String数据
33+
socket.Send(bytes); // 发送byte[]类型数据
2934
3035
// 关闭连接
3136
socket.Close();
3237
```
3338

34-
### 3. 功能说明
39+
- 详细使用方法可参考项目中的Example示例,或参考 [websocket-sharp](https://github.com/sta/websocket-sharp) 的使用方法。
40+
41+
### 3. 模块说明
3542
- WebSocket.jslib
3643

3744
路径:Plugins/WebSocketJS/WebSocketJS.jslib
@@ -40,27 +47,28 @@
4047
- WebSocket.cs
4148

4249
作用:WebSocket连接,可同时创建多个不同连接。
50+
已经支持全平台使用。
4351

4452
- WebSocketReceiver.cs
4553

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

49-
- Demo场景
57+
- Example场景
5058

5159
作用:WebSocket的使用方法示例。
5260

5361
### 4. 注意(Warning)
54-
- WebSocket的命名空间是 UnityWebSocket 不要用错了 :) 。
55-
- WebSocket的 onOpen、OnClose、OnReceive 回调都发生在网络线程中,回调处理函数不能直接修改主线程中的Unity组件内容,需要在主线程中加消息处理队列,缓存网络消息后,再在主线程中处理消息包。
56-
- WebGL平台下,需要发布到Tomcat等服务器上运行。
57-
- ServerDemo 是用于Demo测试版本的WebSocket服务器,兼容所有Release版本的Demo。
58-
- v1.1 后版本有使用websocket-sharp插件,如果本地已使用该插件,可自行修改或删除。
62+
- WebSocket的命名空间是 UnityWebSocket ,项目中有多个命名空间存在WebSocket类,不要用错了 :) 。
63+
- WebSocket的 onOpen、OnClose、OnMessage、OnError 回调都发生在网络线程中,回调处理函数不能直接修改主线程中的Unity组件内容,需要在主线程中加消息处理队列(需要加锁),缓存网络消息后,再在主线程中处理消息包。
64+
- WebGL平台下,暂时不能使用异步连接、关闭、发送,接口仍然使用的同步方式。
65+
- WebGL平台下,需要将打包好的文件,发布到Tomcat等服务器上运行。
66+
- ServerDemo 是用于示例版本的WebSocket测试服务器,需要使用对应的版本。
67+
- v1.1 后版本加入了websocket-sharp插件(源码),如果你的项目已包含该插件,可自行删除或修改。
5968

6069
### 5. WebSocket服务器
61-
- 项目发布完成后,需要一个WebSocket服务器收发消息,以下是Demo版本对应的服务器。
62-
- [服务器Demo下载](https://github.com/y85171642/UnityWebSocket/tree/master/Release/Server)
6370
- 提供简单的WebSocket消息收发
71+
- 每个版本都包含对应的服务器(ServerDemo)。
6472
- 使用了开源项目 [websocket-sharp](https://github.com/sta/websocket-sharp)
6573

6674
### 6. 版本记录
@@ -78,3 +86,11 @@
7886
- 完善项目命名空间,目录结构。
7987
- WebSocket增加异步连接发送方法。(webgl平台下仍调用同步方式)
8088
- 添加开发分支,git管理方式调整。
89+
90+
#### v1.2
91+
- 重构代码,规范代码,模块整理。
92+
- 规范接口,参考websocket-sharp结构,使用EventHandler方式处理事件。
93+
- 添加了字符串数据收发的支持。
94+
- jslib中添加了获取socket.readyState的方法。
95+
- jslib中的SendMessage参数整理。
96+
- fix some Bugs.

UnityWebSocket/Assets/Plugins/WebSocketJS/WebSocketJS.jslib

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
var WebSocketJS =
32
{
43
$RECEIVER_NAME: {},
@@ -55,7 +54,8 @@
5554
webSocket.onerror = function(e)
5655
{
5756
// can not catch the error reason, only use for debug.
58-
OnError(address, e.message);
57+
// see this page https://stackoverflow.com/questions/18803971/websocket-onerror-how-to-read-error-description
58+
OnError(address, "a websocket error occured.");
5959
};
6060

6161
webSocketMap.set(address, webSocket);
@@ -68,7 +68,7 @@
6868
if(webSocketMap.has(address))
6969
webSocketMap.get(address).send(HEAPU8.buffer.slice(msgPtr, msgPtr + length));
7070
else
71-
OnError(address, "send msg with a WebSocket not Instantiated");
71+
OnError(address, "send msg binary with a WebSocket not Instantiated");
7272
},
7373

7474
// call by unity
@@ -79,7 +79,7 @@
7979
if(webSocketMap.has(address))
8080
webSocketMap.get(address).send(msg);
8181
else
82-
OnError(address, "send msg with a WebSocket not Instantiated");
82+
OnError(address, "send msg string with a WebSocket not Instantiated");
8383
},
8484

8585
// call by unity
@@ -105,32 +105,30 @@
105105

106106
$OnMessage: function(address, opcode, data)
107107
{
108-
var addr_opcode_data = address + "_" + opcode + "_";
109-
// blobData
110-
if(opcode == 2)
108+
var combinedMsg = address + "_" + opcode + "_";
109+
if(opcode == 2) // blob data
111110
{
112111
var reader = new FileReader();
113112
reader.addEventListener("loadend", function()
114113
{
115-
// format : address_data, (address and data split with "_")
116-
// the data format is hex string
114+
// data format to hex string
117115
var array = new Uint8Array(reader.result);
118116
for(var i = 0; i < array.length; i++)
119117
{
120118
var b = array[i];
121119
if(b < 16)
122-
addr_opcode_data += "0" + b.toString(16);
120+
combinedMsg += "0" + b.toString(16);
123121
else
124-
addr_opcode_data += b.toString(16);
122+
combinedMsg += b.toString(16);
125123
}
126-
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, addr_opcode_data);
124+
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, combinedMsg);
127125
});
128126
reader.readAsArrayBuffer(data);
129127
}
130-
else
128+
else // string data
131129
{
132-
addr_opcode_data += data;
133-
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, addr_opcode_data);
130+
combinedMsg += data;
131+
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, combinedMsg);
134132
}
135133
},
136134

@@ -143,7 +141,8 @@
143141
{
144142
if(webSocketMap.get(address))
145143
webSocketMap.delete(address);
146-
SendMessage(RECEIVER_NAME, CLOSE_METHOD_NAME, address+"_"+code+"_"+reason+"_"+wasClean);
144+
var combinedMsg = address + "_" + code + "_" + reason + "_" + wasClean;
145+
SendMessage(RECEIVER_NAME, CLOSE_METHOD_NAME, combinedMsg);
147146
},
148147

149148
$OnError: function(address, errorMsg)

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Example/Example.unity

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.37311947, g: 0.38074005, b: 0.35872722, a: 1}
41+
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1}
4242
--- !u!157 &3
4343
LightmapSettings:
4444
m_ObjectHideFlags: 0
@@ -682,7 +682,7 @@ GameObject:
682682
m_Component:
683683
- component: {fileID: 313235359}
684684
m_Layer: 5
685-
m_Name: log
685+
m_Name: messageBox
686686
m_TagString: Untagged
687687
m_Icon: {fileID: 0}
688688
m_NavMeshLayer: 0
@@ -2274,7 +2274,7 @@ GameObject:
22742274
- component: {fileID: 1002391569}
22752275
- component: {fileID: 1002391568}
22762276
m_Layer: 5
2277-
m_Name: bg
2277+
m_Name: mask
22782278
m_TagString: Untagged
22792279
m_Icon: {fileID: 0}
22802280
m_NavMeshLayer: 0
@@ -2847,9 +2847,9 @@ MonoBehaviour:
28472847
entryTemplate: {fileID: 1967433805}
28482848
currentSelectBg: {fileID: 1869076092}
28492849
currentSelectText: {fileID: 533606997}
2850-
logPanel: {fileID: 313235358}
2851-
logText: {fileID: 401804344}
2852-
logPanelCloseBtn: {fileID: 571569540}
2850+
messageBoxObj: {fileID: 313235358}
2851+
messagexBoxText: {fileID: 401804344}
2852+
messageBoxCloseBtn: {fileID: 571569540}
28532853
--- !u!4 &1541134461
28542854
Transform:
28552855
m_ObjectHideFlags: 0
@@ -3482,7 +3482,7 @@ Canvas:
34823482
m_OverrideSorting: 0
34833483
m_OverridePixelPerfect: 0
34843484
m_SortingBucketNormalizedSize: 0
3485-
m_AdditionalShaderChannelsFlag: 25
3485+
m_AdditionalShaderChannelsFlag: 0
34863486
m_SortingLayerID: 0
34873487
m_SortingOrder: 0
34883488
m_TargetDisplay: 0
@@ -3718,7 +3718,7 @@ MonoBehaviour:
37183718
m_HandleRect: {fileID: 837294421}
37193719
m_Direction: 2
37203720
m_Value: 1
3721-
m_Size: 0.99999994
3721+
m_Size: 1
37223722
m_NumberOfSteps: 0
37233723
m_OnValueChanged:
37243724
m_PersistentCalls:

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public class TestWebSocket : MonoBehaviour
1919
public Image currentSelectBg;
2020
public Text currentSelectText;
2121

22-
public GameObject logPanel;
23-
public Text logText;
24-
public Button logPanelCloseBtn;
22+
public GameObject messageBoxObj;
23+
public Text messagexBoxText;
24+
public Button messageBoxCloseBtn;
2525

2626
private Dictionary<string, WebSocketEntry> m_sockets = new Dictionary<string, WebSocketEntry>();
2727
private WebSocketEntry m_selectedEntry;
@@ -32,17 +32,17 @@ private void Awake()
3232
connentBtn.onClick.AddListener(Connect);
3333
closeBtn.onClick.AddListener(Close);
3434
sendBtn.onClick.AddListener(Send);
35-
logPanelCloseBtn.onClick.AddListener(OnClickCloseLog);
35+
messageBoxCloseBtn.onClick.AddListener(OnClickCloseMessageBox);
3636
entryTemplate.gameObject.SetActive(false);
37-
logPanel.gameObject.SetActive(false);
37+
messageBoxObj.gameObject.SetActive(false);
3838
}
3939

4040
public void NewSocket()
4141
{
4242
string addr = addressInput.text;
4343
if (m_sockets.ContainsKey(addr))
4444
{
45-
Log("Duplicate address " + addr);
45+
MessageBox("Duplicate address " + addr);
4646
return;
4747
}
4848

@@ -73,7 +73,6 @@ public void Connect()
7373

7474
public void Close()
7575
{
76-
7776
if (m_selectedEntry == null)
7877
return;
7978
m_selectedEntry.Close();
@@ -86,15 +85,15 @@ public void Send()
8685
m_selectedEntry.Send(messageInput.text);
8786
}
8887

89-
public void Log(string log)
88+
public void MessageBox(string log)
9089
{
91-
logPanel.SetActive(true);
92-
logText.text = log;
90+
messageBoxObj.SetActive(true);
91+
messagexBoxText.text = log;
9392
}
9493

95-
private void OnClickCloseLog()
94+
private void OnClickCloseMessageBox()
9695
{
97-
logPanel.SetActive(false);
96+
messageBoxObj.SetActive(false);
9897
}
9998

10099
void Update()
@@ -195,25 +194,7 @@ private void OnError(object sender, ErrorEventArgs e)
195194
{
196195
content += "[ERROR] " + e.Message + "\n";
197196
}
198-
199-
200-
Vector3[] CreatePoint(Vector3 start, Vector3 end, float distance)
201-
{
202-
Vector3[] retPoint = new Vector3[4];
203-
204-
Vector3 v3 = Vector3.Cross(end - start, Vector3.up);
205-
206-
retPoint[0] = start + v3 * distance;
207-
retPoint[1] = start - v3 * distance;
208-
retPoint[3] = end + v3 * distance;
209-
retPoint[2] = end - v3 * distance;
210-
211-
return retPoint;
212-
}
213-
214197
}
215-
216-
217198
}
218199

219200

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/websocket-jslib/WebSocketReceiver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void RemoveListener(string address)
6363
/// <summary>
6464
/// jslib will call this method on message received.
6565
/// </summary>
66-
/// <param name="address_data">address_opcode_data(hex string)</param>
66+
/// <param name="address_opcode_data">address_opcode_data(hex string)</param>
6767
private void OnMessage(string address_opcode_data)
6868
{
6969
string[] sp;
@@ -100,7 +100,7 @@ private void OnOpen(string address)
100100
/// <summary>
101101
/// jslib will call this method on connection closed.
102102
/// </summary>
103-
/// <param name="address">address</param>
103+
/// <param name="address_code_reason_wasClean">address_code_reason_wasClean</param>
104104
private void OnClose(string address_code_reason_wasClean)
105105
{
106106
string[] sp;

0 commit comments

Comments
 (0)