Skip to content

Commit f97b33b

Browse files
committed
Update TCPSocket.cs
1 parent 98e286c commit f97b33b

File tree

1 file changed

+80
-48
lines changed

1 file changed

+80
-48
lines changed

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

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class TCPSocket : Details
77
{
88
private WXTCPSocket _tcpSocket;
9+
private bool _connected = false;
910

1011
// 数据
1112
private string _stringData1 = "String Data";
@@ -16,73 +17,104 @@ public class TCPSocket : Details
1617
private byte[] _bufferData2 = { 0xab, 0x05, 0xd7, 0x05 };
1718
private byte[] _bufferData3 = new byte[8];
1819

19-
private int[] _bufferData4 = { 66, 117, 102, 102, 101, 114, 32, 68, 97, 116, 97, 32 };
20-
private int[] _bufferData5 = { 0xab, 0x05, 0xd7, 0x05 };
21-
private int[] _bufferData6 = new int[8];
22-
23-
2420
private void Start() {
25-
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
26-
GameManager.Instance.detailsController.BindExtraButtonAction(1, close);
27-
GameManager.Instance.detailsController.BindExtraButtonAction(2, write);
28-
GameManager.Instance.detailsController.BindExtraButtonAction(3, writeBuffer);
21+
GameManager.Instance.detailsController.BindExtraButtonAction(0, connect);
22+
GameManager.Instance.detailsController.BindExtraButtonAction(1, write);
23+
GameManager.Instance.detailsController.BindExtraButtonAction(2, writeBuffer);
24+
GameManager.Instance.detailsController.BindExtraButtonAction(3, close);
2925
}
3026

3127
// 测试 API
3228
protected override void TestAPI(string[] args)
3329
{
34-
_tcpSocket = WX.CreateTCPSocket();
35-
Debug.Log("tcpSocket: " + JsonUtility.ToJson(_tcpSocket));
30+
if(_tcpSocket == null)
31+
{
32+
_tcpSocket = WX.CreateTCPSocket();
33+
Debug.Log("tcpSocket: " + JsonUtility.ToJson(_tcpSocket));
3634

37-
_tcpSocket.OnMessage((res) => {
38-
Debug.Log("onMessage: " + JsonUtility.ToJson(res));
39-
});
35+
_tcpSocket.OnMessage((res) => {
36+
Debug.Log("onMessage: " + JsonUtility.ToJson(res));
37+
});
4038

41-
_tcpSocket.OnConnect((res) => {
42-
Debug.Log("onConnect: " + JsonUtility.ToJson(res));
43-
});
39+
_tcpSocket.OnConnect((res) => {
40+
Debug.Log("onConnect: " + JsonUtility.ToJson(res));
41+
});
4442

45-
_tcpSocket.OnError((res) => {
46-
Debug.Log("onError: " + JsonUtility.ToJson(res));
47-
});
43+
_tcpSocket.OnError((res) => {
44+
Debug.Log("onError: " + JsonUtility.ToJson(res));
45+
});
4846

49-
_tcpSocket.OnClose((res) => {
50-
Debug.Log("onClose: " + JsonUtility.ToJson(res));
51-
});
47+
_tcpSocket.OnClose((res) => {
48+
Debug.Log("onClose: " + JsonUtility.ToJson(res));
49+
});
50+
} else
51+
{
52+
Debug.Log("tcp实例已初始化");
53+
}
54+
5255
}
5356

54-
private void connect() {
55-
Debug.Log("connect test start");
56-
_tcpSocket.Connect(new TCPSocketConnectOption() {
57-
address = "www.oooceanworld.com", port = 8101
58-
});
57+
private void close()
58+
{
59+
if(_tcpSocket != null && _connected)
60+
{
61+
Debug.Log("close test start");
62+
_tcpSocket.Close();
63+
_connected = false;
64+
} else
65+
{
66+
Debug.Log("关闭失败:tcp实例未初始化或未连接");
67+
}
68+
5969
}
6070

61-
private void close() {
62-
Debug.Log("close test start");
63-
_tcpSocket.Close();
71+
72+
73+
private void connect() {
74+
if (_tcpSocket != null && !_connected) {
75+
Debug.Log("connect test start");
76+
_tcpSocket.Connect(new TCPSocketConnectOption()
77+
{
78+
address = "www.oooceanworld.com",
79+
port = 8101
80+
});
81+
_connected = true;
82+
} else
83+
{
84+
Debug.Log("连接失败:tcp实例未初始化或已连接");
85+
}
6486
}
6587

6688
private void write() {
67-
Debug.Log("write string test start: "+_stringData);
68-
_tcpSocket.Write(_stringData);
89+
if (_tcpSocket != null && _connected)
90+
{
91+
Debug.Log("write string test start:");
92+
Debug.Log("test 1: " + _stringData1);
93+
_tcpSocket.Write(_stringData1);
94+
Debug.Log("test 2: " + _stringData2);
95+
_tcpSocket.Write(_stringData2);
96+
} else
97+
{
98+
Debug.Log("发送失败:tcp实例未初始化或未连接");
99+
}
100+
69101
}
70102

71-
private void writeBuffer()
72-
{
73-
Debug.Log("write buffer test start:");
74-
Debug.Log("test 1: "+_bufferData1);
75-
_tcpSocket.Write(_bufferData1);
76-
Debug.Log("test 2: " + _bufferData2);
77-
_tcpSocket.Write(_bufferData2);
78-
Debug.Log("test 3: " + _bufferData3);
79-
_tcpSocket.Write(_bufferData3);
80-
Debug.Log("test 4: " + _bufferData4);
81-
_tcpSocket.Write(_bufferData4);
82-
Debug.Log("test 5: " + _bufferData5);
83-
_tcpSocket.Write(_bufferData5);
84-
Debug.Log("test 6: " + _bufferData6);
85-
_tcpSocket.Write(_bufferData6);
103+
private void writeBuffer() {
104+
if (_tcpSocket != null && _connected)
105+
{
106+
Debug.Log("write buffer test start:");
107+
Debug.Log("test 1: " + _bufferData1);
108+
_tcpSocket.Write(_bufferData1);
109+
Debug.Log("test 2: " + _bufferData2);
110+
_tcpSocket.Write(_bufferData2);
111+
Debug.Log("test 3: " + _bufferData3);
112+
_tcpSocket.Write(_bufferData3);
113+
}
114+
else
115+
{
116+
Debug.Log("发送失败:tcp实例未初始化或未连接");
117+
}
86118
}
87119
}
88120

0 commit comments

Comments
 (0)