Skip to content

Commit 767ebbb

Browse files
committed
Improve tcp and udp demo, more function tests supported
1 parent 0782ddb commit 767ebbb

File tree

5 files changed

+143
-59
lines changed

5 files changed

+143
-59
lines changed

Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocket.cs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ public class TCPSocket : Details
1818
private byte[] _bufferData3 = new byte[8];
1919

2020
private void Start() {
21-
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
21+
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
2222
GameManager.Instance.detailsController.BindExtraButtonAction(1, write);
23-
GameManager.Instance.detailsController.BindExtraButtonAction(2, writeBuffer);
24-
GameManager.Instance.detailsController.BindExtraButtonAction(3, close);
23+
GameManager.Instance.detailsController.BindExtraButtonAction(2, close);
2524
}
2625

2726
// 测试 API
@@ -58,7 +57,6 @@ private void close()
5857
{
5958
if(_tcpSocket != null && _connected)
6059
{
61-
Debug.Log("close test start");
6260
_tcpSocket.Close();
6361
_connected = false;
6462
} else
@@ -71,7 +69,6 @@ private void close()
7169

7270
private void connect() {
7371
if (_tcpSocket != null && !_connected) {
74-
Debug.Log("connect test start");
7572
_tcpSocket.Connect(new TCPSocketConnectOption()
7673
{
7774
address = "www.oooceanworld.com",
@@ -87,33 +84,26 @@ private void connect() {
8784
private void write() {
8885
if (_tcpSocket != null && _connected)
8986
{
90-
Debug.Log("write string test start:");
91-
Debug.Log("test 1: " + _stringData1);
92-
_tcpSocket.Write(_stringData1);
93-
Debug.Log("test 2: " + _stringData2);
94-
_tcpSocket.Write(_stringData2);
87+
if (options[0] == "String")
88+
{
89+
Debug.Log("test 1: " + _stringData1);
90+
_tcpSocket.Write(_stringData1);
91+
Debug.Log("test 2: " + _stringData2);
92+
_tcpSocket.Write(_stringData2);
93+
}
94+
else
95+
{
96+
Debug.Log("test 1: " + _bufferData1);
97+
_tcpSocket.Write(_bufferData1);
98+
Debug.Log("test 2: " + _bufferData2);
99+
_tcpSocket.Write(_bufferData2);
100+
Debug.Log("test 3: " + _bufferData3);
101+
_tcpSocket.Write(_bufferData3);
102+
}
95103
} else
96104
{
97105
Debug.Log("发送失败:tcp实例未初始化或未连接");
98106
}
99-
100-
}
101-
102-
private void writeBuffer() {
103-
if (_tcpSocket != null && _connected)
104-
{
105-
Debug.Log("write buffer test start:");
106-
Debug.Log("test 1: " + _bufferData1);
107-
_tcpSocket.Write(_bufferData1);
108-
Debug.Log("test 2: " + _bufferData2);
109-
_tcpSocket.Write(_bufferData2);
110-
Debug.Log("test 3: " + _bufferData3);
111-
_tcpSocket.Write(_bufferData3);
112-
}
113-
else
114-
{
115-
Debug.Log("发送失败:tcp实例未初始化或未连接");
116-
}
117107
}
118108
}
119109

Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocketSO.asset

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ MonoBehaviour:
1515
entryScriptTypeName: TCPSocket
1616
entryName: "TCP\u901A\u4FE1"
1717
entryAPI: "TCP \u901A\u4FE1\u76F8\u5173api"
18-
entryDescription: "\u9700\u8981\u586B\u5165ip\u548Cport"
19-
optionList: []
18+
entryDescription: "\u9700\u8981\u5148\u521B\u5EFA\u5B9E\u4F8B\u5E76\u5EFA\u7ACB\u8FDE\u63A5\u624D\u80FD\u53D1\u9001\u4FE1\u606F"
19+
optionList:
20+
- optionName: "\u53D1\u9001\u4FE1\u606F\u7C7B\u578B"
21+
availableOptions:
22+
- String
23+
- Buffer
2024
initialButtonText: "\u521B\u5EFAtcp\u5B9E\u4F8B"
2125
extraButtonList:
2226
- buttonText: "\u8FDE\u63A5"
23-
- buttonText: "\u53D1\u9001String"
24-
- buttonText: "\u53D1\u9001Buffer"
27+
- buttonText: "\u53D1\u9001"
2528
- buttonText: "\u5173\u95ED"
2629
initialResultList: []

Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocket.cs

Lines changed: 104 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,129 @@
66
public class UDPSocket : Details
77
{
88
private WXUDPSocket _udpSocket;
9+
private bool _connected = false;
10+
11+
// 数据
12+
private string _stringData = "hello, how are you";
13+
private byte[] _bufferData = { 66, 117, 102, 102, 101, 114, 32, 68, 97, 116, 97, 32 };
914

1015
private void Start() {
1116
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
12-
GameManager.Instance.detailsController.BindExtraButtonAction(1, close);
17+
GameManager.Instance.detailsController.BindExtraButtonAction(1, write);
18+
GameManager.Instance.detailsController.BindExtraButtonAction(2, send);
19+
GameManager.Instance.detailsController.BindExtraButtonAction(3, close);
1320
}
1421

1522
// 测试 API
1623
protected override void TestAPI(string[] args)
1724
{
18-
_udpSocket = WX.CreateUDPSocket();
19-
var port = _udpSocket.Bind();
25+
if(_udpSocket == null)
26+
{
27+
_udpSocket = WX.CreateUDPSocket();
28+
var port = _udpSocket.Bind();
2029

21-
Debug.Log("udpSocket: " + JsonUtility.ToJson(_udpSocket));
30+
Debug.Log("udpSocket: " + JsonUtility.ToJson(_udpSocket));
2231

23-
_udpSocket.OnListening((res) => {
24-
Debug.Log("onListening: " + JsonUtility.ToJson(res));
25-
});
32+
_udpSocket.OnListening((res) => {
33+
Debug.Log("onListening: " + JsonUtility.ToJson(res));
34+
});
2635

27-
_udpSocket.OnError((res) => {
28-
Debug.Log("onError: " + JsonUtility.ToJson(res));
29-
});
36+
_udpSocket.OnError((res) => {
37+
Debug.Log("onError: " + JsonUtility.ToJson(res));
38+
});
3039

31-
_udpSocket.OnClose((res) => {
32-
Debug.Log("onClose: " + JsonUtility.ToJson(res));
33-
});
40+
_udpSocket.OnClose((res) => {
41+
Debug.Log("onClose: " + JsonUtility.ToJson(res));
42+
});
3443

35-
_udpSocket.OnMessage((res) => {
36-
Debug.Log("onMessage: " + JsonUtility.ToJson(res));
37-
});
44+
_udpSocket.OnMessage((res) => {
45+
Debug.Log("onMessage: " + JsonUtility.ToJson(res));
46+
});
47+
}
48+
else
49+
{
50+
Debug.LogError("udp实例已初始化");
51+
}
3852
}
3953

4054
private void connect() {
41-
_udpSocket.Connect(new UDPSocketConnectOption() {
42-
address = "192.168.193.2",
43-
port = 8848
44-
});
55+
if (_udpSocket != null && !_connected)
56+
{
57+
_udpSocket.Connect(new UDPSocketConnectOption()
58+
{
59+
address = "www.oooceanworld.com",
60+
port = 8101
61+
});
62+
_connected = true;
63+
} else
64+
{
65+
Debug.LogError("连接失败:udp实例未初始化或已连接");
66+
}
67+
}
68+
69+
private void write()
70+
{
71+
if (_udpSocket != null && _connected)
72+
{
73+
Debug.LogError("接口有bug暂未修复 当前为placeholder");
74+
/*
75+
UDPSocketWriteOption option = new UDPSocketWriteOption()
76+
{
77+
address = "www.oooceanworld.com",
78+
port = 8101
79+
};
80+
if (options[0] == "String")
81+
{
82+
option.message = _stringData;
83+
}
84+
else
85+
{
86+
option.message = _bufferData;
87+
}
88+
_udpSocket.Write(option);
89+
*/
90+
}
91+
else
92+
{
93+
Debug.LogError("write失败:udp实例未初始化或未连接");
94+
}
95+
}
96+
97+
private void send()
98+
{
99+
if (_udpSocket != null)
100+
{
101+
UDPSocketSendOption option = new UDPSocketSendOption()
102+
{
103+
address = "www.oooceanworld.com",
104+
port = 8101
105+
};
106+
if (options[0] == "String")
107+
{
108+
option.message = _stringData;
109+
}
110+
else
111+
{
112+
option.message = _bufferData;
113+
}
114+
_udpSocket.Send(option);
115+
Debug.Log("Message: " + option.message);
116+
}
117+
else
118+
{
119+
Debug.LogError("send失败:udp实例未初始化");
120+
}
45121
}
46122

47123
private void close() {
48-
_udpSocket.Close();
124+
if (_udpSocket != null && _connected)
125+
{
126+
_udpSocket.Close();
127+
_connected = false;
128+
} else
129+
{
130+
Debug.LogError("关闭失败:udp实例未初始化或未连接");
131+
}
49132
}
50133
}
51134

Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocketSO.asset

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ MonoBehaviour:
1515
entryScriptTypeName: UDPSocket
1616
entryName: "UDP\u901A\u4FE1"
1717
entryAPI: "UDP \u901A\u4FE1\u76F8\u5173api"
18-
entryDescription: "\u9700\u8981\u586B\u5165ip\u548Cport"
19-
optionList: []
18+
entryDescription: "write\u7528\u6CD5\u4E0E send \u65B9\u6CD5\u76F8\u540C\uFF0C\u5982\u679C\u6CA1\u6709\u9884\u5148\u8C03\u7528
19+
connect \u5219\u4E0E send \u65E0\u5DEE\u5F02\uFF08\u6CE8\u610F\u5373\u4F7F\u8C03\u7528\u4E86
20+
connect \u4E5F\u9700\u8981\u5728\u672C\u63A5\u53E3\u586B\u5165\u5730\u5740\u548C\u7AEF\u53E3\u53C2\u6570\uFF09"
21+
optionList:
22+
- optionName: "\u53D1\u9001\u4FE1\u606F\u7C7B\u578B"
23+
availableOptions:
24+
- String
25+
- Buffer
2026
initialButtonText: "\u521B\u5EFAudp\u5B9E\u4F8B"
2127
extraButtonList:
2228
- buttonText: "\u8FDE\u63A5"
29+
- buttonText: Write
30+
- buttonText: Send
2331
- buttonText: "\u5173\u95ED"
2432
initialResultList: []

Demo/API_V2/Assets/WX-WASM-SDK-V2/Editor/MiniGameConfig.asset

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ MonoBehaviour:
1313
m_Name: MiniGameConfig
1414
m_EditorClassIdentifier:
1515
ProjectConf:
16-
projectName: GameClubDemo
16+
projectName: NetworkDemo
1717
Appid: wxb7d4485dbc1233ca
18-
CDN:
19-
assetLoadType: 1
18+
CDN: http://127.0.0.1:8080
19+
assetLoadType: 0
2020
compressDataPackage: 0
2121
VideoUrl:
22-
DST: C:/Users/xiaoyuejin/Desktop/finaltest
22+
DST: C:/Users/xiaoyuejin/Desktop/Network
2323
StreamCDN:
2424
bundleHashLength: 32
2525
bundlePathIdentifier: StreamingAssets;

0 commit comments

Comments
 (0)