Skip to content

更新api_v2中的网络板块 #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ docs
node_modules
.config/.vitepress/dist
.config/.vitepress/cache
Demo/API_V2/Assets/WX-WASM-SDK-V2/Editor/MiniGameConfig.asset
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/APISO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ MonoBehaviour:
- {fileID: 11400000, guid: 948907759756641618ba1b031955ec2b, type: 2}
- {fileID: 11400000, guid: 14a1a853f10124ee2b276992e2d40448, type: 2}
- {fileID: 11400000, guid: 1aa518b2f8ca04c6e81821bcb9a3cc49, type: 2}
- {fileID: 11400000, guid: 89339dab17a614cbf8abc8469d67cb72, type: 2}
- {fileID: 11400000, guid: 27654a238f98e4f7e8756e4caed418e1, type: 2}
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/NetworkSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ MonoBehaviour:
- {fileID: 11400000, guid: 8fb54c5918d454eea90d5147f2c316ea, type: 2}
- {fileID: 11400000, guid: 1e0539509d074443f92d17035efbe40f, type: 2}
- {fileID: 11400000, guid: cdc97aef7249c4216ac09d2321ce8e83, type: 2}
- {fileID: 11400000, guid: 529e27dae48c91748a3fc5f65603e605, type: 2}
- {fileID: 11400000, guid: 8bae636d6ff994bbfba56ae723a63862, type: 2}
58 changes: 26 additions & 32 deletions Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using LitJson;
using UnityEngine;
using WeChatWASM;

public class TCPSocket : Details
{
private WXTCPSocket _tcpSocket;

private bool _connected = false;

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

private void Start() {
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
GameManager.Instance.detailsController.BindExtraButtonAction(1, write);
GameManager.Instance.detailsController.BindExtraButtonAction(2, writeBuffer);
GameManager.Instance.detailsController.BindExtraButtonAction(3, close);
GameManager.Instance.detailsController.BindExtraButtonAction(2, close);
}

// 测试 API
Expand All @@ -30,6 +31,7 @@ protected override void TestAPI(string[] args)
if(_tcpSocket == null)
{
_tcpSocket = WX.CreateTCPSocket();

Debug.Log("tcpSocket: " + JsonUtility.ToJson(_tcpSocket));

_tcpSocket.OnMessage((res) => {
Expand All @@ -49,7 +51,7 @@ protected override void TestAPI(string[] args)
});
} else
{
Debug.Log("tcp实例已初始化");
Debug.LogError("tcp实例已初始化");
}

}
Expand All @@ -58,20 +60,19 @@ private void close()
{
if(_tcpSocket != null && _connected)
{
Debug.Log("close test start");
_tcpSocket.Close();
_connected = false;
_tcpSocket = null;
} else
{
Debug.Log("关闭失败:tcp实例未初始化或未连接");
Debug.LogError("关闭失败:tcp实例未初始化或未连接");
}

}


private void connect() {
if (_tcpSocket != null && !_connected) {
Debug.Log("connect test start");
_tcpSocket.Connect(new TCPSocketConnectOption()
{
address = "www.oooceanworld.com",
Expand All @@ -80,39 +81,32 @@ private void connect() {
_connected = true;
} else
{
Debug.Log("连接失败:tcp实例未初始化或已连接");
Debug.LogError("连接失败:tcp实例未初始化或已连接");
}
}

private void write() {
if (_tcpSocket != null && _connected)
{
Debug.Log("write string test start:");
Debug.Log("test 1: " + _stringData1);
_tcpSocket.Write(_stringData1);
Debug.Log("test 2: " + _stringData2);
_tcpSocket.Write(_stringData2);
if (options[0] == "String")
{
Debug.Log("test 1: " + _stringData1);
_tcpSocket.Write(_stringData1);
Debug.Log("test 2: " + _stringData2);
_tcpSocket.Write(_stringData2);
}
else
{
Debug.Log("test 1: " + _bufferData1);
_tcpSocket.Write(_bufferData1);
Debug.Log("test 2: " + _bufferData2);
_tcpSocket.Write(_bufferData2);
Debug.Log("test 3: " + _bufferData3);
_tcpSocket.Write(_bufferData3);
}
} else
{
Debug.Log("发送失败:tcp实例未初始化或未连接");
}

}

private void writeBuffer() {
if (_tcpSocket != null && _connected)
{
Debug.Log("write buffer test start:");
Debug.Log("test 1: " + _bufferData1);
_tcpSocket.Write(_bufferData1);
Debug.Log("test 2: " + _bufferData2);
_tcpSocket.Write(_bufferData2);
Debug.Log("test 3: " + _bufferData3);
_tcpSocket.Write(_bufferData3);
}
else
{
Debug.Log("发送失败:tcp实例未初始化或未连接");
Debug.LogError("发送失败:tcp实例未初始化或未连接");
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocketSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ MonoBehaviour:
entryScriptTypeName: TCPSocket
entryName: "TCP\u901A\u4FE1"
entryAPI: "TCP \u901A\u4FE1\u76F8\u5173api"
entryDescription: "\u9700\u8981\u586B\u5165ip\u548Cport"
optionList: []
entryDescription: "\u9700\u8981\u5148\u521B\u5EFA\u5B9E\u4F8B\u5E76\u5EFA\u7ACB\u8FDE\u63A5\u624D\u80FD\u53D1\u9001\u4FE1\u606F"
optionList:
- optionName: "\u53D1\u9001\u4FE1\u606F\u7C7B\u578B"
availableOptions:
- String
- Buffer
initialButtonText: "\u521B\u5EFAtcp\u5B9E\u4F8B"
extraButtonList:
- buttonText: "\u8FDE\u63A5"
- buttonText: "\u53D1\u9001String"
- buttonText: "\u53D1\u9001Buffer"
- buttonText: "\u53D1\u9001"
- buttonText: "\u5173\u95ED"
initialResultList: []
126 changes: 105 additions & 21 deletions Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,130 @@
public class UDPSocket : Details
{
private WXUDPSocket _udpSocket;
private bool _connected = false;

// 数据
private string _stringData = "hello, how are you";
private byte[] _bufferData = { 66, 117, 102, 102, 101, 114, 32, 68, 97, 116, 97, 32 };

private void Start() {
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
GameManager.Instance.detailsController.BindExtraButtonAction(1, close);
GameManager.Instance.detailsController.BindExtraButtonAction(1, write);
GameManager.Instance.detailsController.BindExtraButtonAction(2, send);
GameManager.Instance.detailsController.BindExtraButtonAction(3, close);
}

// 测试 API
protected override void TestAPI(string[] args)
{
_udpSocket = WX.CreateUDPSocket();
var port = _udpSocket.Bind();
if(_udpSocket == null)
{
_udpSocket = WX.CreateUDPSocket();
var port = _udpSocket.Bind();

Debug.Log("udpSocket: " + JsonUtility.ToJson(_udpSocket));
Debug.Log("udpSocket: " + JsonUtility.ToJson(_udpSocket));

_udpSocket.OnListening((res) => {
Debug.Log("onListening: " + JsonUtility.ToJson(res));
});
_udpSocket.OnListening((res) => {
Debug.Log("onListening: " + JsonUtility.ToJson(res));
});

_udpSocket.OnError((res) => {
Debug.Log("onError: " + JsonUtility.ToJson(res));
});
_udpSocket.OnError((res) => {
Debug.Log("onError: " + JsonUtility.ToJson(res));
});

_udpSocket.OnClose((res) => {
Debug.Log("onClose: " + JsonUtility.ToJson(res));
});
_udpSocket.OnClose((res) => {
Debug.Log("onClose: " + JsonUtility.ToJson(res));
});

_udpSocket.OnMessage((res) => {
Debug.Log("onMessage: " + JsonUtility.ToJson(res));
});
_udpSocket.OnMessage((res) => {
Debug.Log("onMessage: " + JsonUtility.ToJson(res));
});
}
else
{
Debug.LogError("udp实例已初始化");
}
}

private void connect() {
_udpSocket.Connect(new UDPSocketConnectOption() {
address = "192.168.193.2",
port = 8848
});
if (_udpSocket != null && !_connected)
{
_udpSocket.Connect(new UDPSocketConnectOption()
{
address = "www.oooceanworld.com",
port = 8101
});
_connected = true;
} else
{
Debug.LogError("连接失败:udp实例未初始化或已连接");
}
}

private void write()
{
if (_udpSocket != null && _connected)
{
Debug.LogError("接口有bug暂未修复 当前为placeholder");
/*
UDPSocketWriteOption option = new UDPSocketWriteOption()
{
address = "www.oooceanworld.com",
port = 8101
};
if (options[0] == "String")
{
option.message = _stringData;
}
else
{
option.message = _bufferData;
}
_udpSocket.Write(option);
*/
}
else
{
Debug.LogError("write失败:udp实例未初始化或未连接");
}
}

private void send()
{
if (_udpSocket != null)
{
UDPSocketSendOption option = new UDPSocketSendOption()
{
address = "www.oooceanworld.com",
port = 8101
};
if (options[0] == "String")
{
option.message = _stringData;
}
else
{
option.message = _bufferData;
}
_udpSocket.Send(option);
Debug.Log("Message: " + option.message);
}
else
{
Debug.LogError("send失败:udp实例未初始化");
}
}

private void close() {
_udpSocket.Close();
if (_udpSocket != null && _connected)
{
_udpSocket.Close();
_connected = false;
_udpSocket = null;
} else
{
Debug.LogError("关闭失败:udp实例未初始化或未连接");
}
}
}

12 changes: 10 additions & 2 deletions Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocketSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ MonoBehaviour:
entryScriptTypeName: UDPSocket
entryName: "UDP\u901A\u4FE1"
entryAPI: "UDP \u901A\u4FE1\u76F8\u5173api"
entryDescription: "\u9700\u8981\u586B\u5165ip\u548Cport"
optionList: []
entryDescription: "write\u7528\u6CD5\u4E0E send \u65B9\u6CD5\u76F8\u540C\uFF0C\u5982\u679C\u6CA1\u6709\u9884\u5148\u8C03\u7528
connect \u5219\u4E0E send \u65E0\u5DEE\u5F02\uFF08\u6CE8\u610F\u5373\u4F7F\u8C03\u7528\u4E86
connect \u4E5F\u9700\u8981\u5728\u672C\u63A5\u53E3\u586B\u5165\u5730\u5740\u548C\u7AEF\u53E3\u53C2\u6570\uFF09"
optionList:
- optionName: "\u53D1\u9001\u4FE1\u606F\u7C7B\u578B"
availableOptions:
- String
- Buffer
initialButtonText: "\u521B\u5EFAudp\u5B9E\u4F8B"
extraButtonList:
- buttonText: "\u8FDE\u63A5"
- buttonText: Write
- buttonText: Send
- buttonText: "\u5173\u95ED"
initialResultList: []
8 changes: 8 additions & 0 deletions Demo/API_V2/Assets/API/Network/UnityWebRequest.meta

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

24 changes: 24 additions & 0 deletions Demo/API_V2/Assets/API/Network/UnityWebRequest/WebRequest SO.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
m_Name: WebRequest SO
m_EditorClassIdentifier:
entryScriptTypeName: WebRequest
entryName: "HTTP \u901A\u4FE1"
entryAPI: UnityWebRequest
entryDescription:
optionList: []
initialButtonText: Put
extraButtonList:
- buttonText: Post
- buttonText: Get
initialResultList: []

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

Loading
Loading