Skip to content

Commit d03ee1f

Browse files
authored
Merge pull request #831 from wechat-miniprogram/UDPSocket_Demo
更新api_v2中的网络板块
2 parents 0848b7f + 6d86a39 commit d03ee1f

23 files changed

+330
-99
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ docs
5656
node_modules
5757
.config/.vitepress/dist
5858
.config/.vitepress/cache
59+
Demo/API_V2/Assets/WX-WASM-SDK-V2/Editor/MiniGameConfig.asset

Diff for: Demo/API_V2/Assets/API/APISO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ MonoBehaviour:
3434
- {fileID: 11400000, guid: 948907759756641618ba1b031955ec2b, type: 2}
3535
- {fileID: 11400000, guid: 14a1a853f10124ee2b276992e2d40448, type: 2}
3636
- {fileID: 11400000, guid: 1aa518b2f8ca04c6e81821bcb9a3cc49, type: 2}
37+
- {fileID: 11400000, guid: 89339dab17a614cbf8abc8469d67cb72, type: 2}
3738
- {fileID: 11400000, guid: 27654a238f98e4f7e8756e4caed418e1, type: 2}

Diff for: Demo/API_V2/Assets/API/Network/NetworkSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ MonoBehaviour:
1818
- {fileID: 11400000, guid: 8fb54c5918d454eea90d5147f2c316ea, type: 2}
1919
- {fileID: 11400000, guid: 1e0539509d074443f92d17035efbe40f, type: 2}
2020
- {fileID: 11400000, guid: cdc97aef7249c4216ac09d2321ce8e83, type: 2}
21+
- {fileID: 11400000, guid: 529e27dae48c91748a3fc5f65603e605, type: 2}
2122
- {fileID: 11400000, guid: 8bae636d6ff994bbfba56ae723a63862, type: 2}

Diff for: Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocket.cs

+26-32
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using LitJson;
44
using UnityEngine;
55
using WeChatWASM;
6+
67
public class TCPSocket : Details
78
{
89
private WXTCPSocket _tcpSocket;
10+
911
private bool _connected = false;
1012

1113
// 数据
@@ -18,10 +20,9 @@ public class TCPSocket : Details
1820
private byte[] _bufferData3 = new byte[8];
1921

2022
private void Start() {
21-
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
23+
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
2224
GameManager.Instance.detailsController.BindExtraButtonAction(1, write);
23-
GameManager.Instance.detailsController.BindExtraButtonAction(2, writeBuffer);
24-
GameManager.Instance.detailsController.BindExtraButtonAction(3, close);
25+
GameManager.Instance.detailsController.BindExtraButtonAction(2, close);
2526
}
2627

2728
// 测试 API
@@ -30,6 +31,7 @@ protected override void TestAPI(string[] args)
3031
if(_tcpSocket == null)
3132
{
3233
_tcpSocket = WX.CreateTCPSocket();
34+
3335
Debug.Log("tcpSocket: " + JsonUtility.ToJson(_tcpSocket));
3436

3537
_tcpSocket.OnMessage((res) => {
@@ -49,7 +51,7 @@ protected override void TestAPI(string[] args)
4951
});
5052
} else
5153
{
52-
Debug.Log("tcp实例已初始化");
54+
Debug.LogError("tcp实例已初始化");
5355
}
5456

5557
}
@@ -58,20 +60,19 @@ private void close()
5860
{
5961
if(_tcpSocket != null && _connected)
6062
{
61-
Debug.Log("close test start");
6263
_tcpSocket.Close();
6364
_connected = false;
65+
_tcpSocket = null;
6466
} else
6567
{
66-
Debug.Log("关闭失败:tcp实例未初始化或未连接");
68+
Debug.LogError("关闭失败:tcp实例未初始化或未连接");
6769
}
6870

6971
}
7072

7173

7274
private void connect() {
7375
if (_tcpSocket != null && !_connected) {
74-
Debug.Log("connect test start");
7576
_tcpSocket.Connect(new TCPSocketConnectOption()
7677
{
7778
address = "www.oooceanworld.com",
@@ -80,39 +81,32 @@ private void connect() {
8081
_connected = true;
8182
} else
8283
{
83-
Debug.Log("连接失败:tcp实例未初始化或已连接");
84+
Debug.LogError("连接失败:tcp实例未初始化或已连接");
8485
}
8586
}
8687

8788
private void write() {
8889
if (_tcpSocket != null && _connected)
8990
{
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);
91+
if (options[0] == "String")
92+
{
93+
Debug.Log("test 1: " + _stringData1);
94+
_tcpSocket.Write(_stringData1);
95+
Debug.Log("test 2: " + _stringData2);
96+
_tcpSocket.Write(_stringData2);
97+
}
98+
else
99+
{
100+
Debug.Log("test 1: " + _bufferData1);
101+
_tcpSocket.Write(_bufferData1);
102+
Debug.Log("test 2: " + _bufferData2);
103+
_tcpSocket.Write(_bufferData2);
104+
Debug.Log("test 3: " + _bufferData3);
105+
_tcpSocket.Write(_bufferData3);
106+
}
95107
} else
96108
{
97-
Debug.Log("发送失败:tcp实例未初始化或未连接");
98-
}
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实例未初始化或未连接");
109+
Debug.LogError("发送失败:tcp实例未初始化或未连接");
116110
}
117111
}
118112
}

Diff for: Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocketSO.asset

+7-4
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: []

Diff for: Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocket.cs

+105-21
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,130 @@
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+
_udpSocket = null;
129+
} else
130+
{
131+
Debug.LogError("关闭失败:udp实例未初始化或未连接");
132+
}
49133
}
50134
}
51135

Diff for: Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocketSO.asset

+10-2
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: []

Diff for: Demo/API_V2/Assets/API/Network/UnityWebRequest.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
13+
m_Name: WebRequest SO
14+
m_EditorClassIdentifier:
15+
entryScriptTypeName: WebRequest
16+
entryName: "HTTP \u901A\u4FE1"
17+
entryAPI: UnityWebRequest
18+
entryDescription:
19+
optionList: []
20+
initialButtonText: Put
21+
extraButtonList:
22+
- buttonText: Post
23+
- buttonText: Get
24+
initialResultList: []

Diff for: Demo/API_V2/Assets/API/Network/UnityWebRequest/WebRequest SO.asset.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)