|
6 | 6 | public class TCPSocket : Details
|
7 | 7 | {
|
8 | 8 | private WXTCPSocket _tcpSocket;
|
| 9 | + private bool _connected = false; |
| 10 | + |
| 11 | + // 数据 |
| 12 | + private string _stringData1 = "String Data"; |
| 13 | + private string _stringData2 = "123\n"; |
| 14 | + |
| 15 | + |
| 16 | + private byte[] _bufferData1 = { 66, 117, 102, 102, 101, 114, 32, 68, 97, 116, 97, 32 }; |
| 17 | + private byte[] _bufferData2 = { 0xab, 0x05, 0xd7, 0x05 }; |
| 18 | + private byte[] _bufferData3 = new byte[8]; |
9 | 19 |
|
10 | 20 | private void Start() {
|
11 |
| - GameManager.Instance.detailsController.BindExtraButtonAction(0, connect); |
12 |
| - GameManager.Instance.detailsController.BindExtraButtonAction(1, close); |
| 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); |
13 | 25 | }
|
14 | 26 |
|
15 | 27 | // 测试 API
|
16 | 28 | protected override void TestAPI(string[] args)
|
17 | 29 | {
|
18 |
| - _tcpSocket = WX.CreateTCPSocket(); |
19 |
| - Debug.Log("tcpSocket: " + JsonUtility.ToJson(_tcpSocket)); |
| 30 | + if(_tcpSocket == null) |
| 31 | + { |
| 32 | + _tcpSocket = WX.CreateTCPSocket(); |
| 33 | + Debug.Log("tcpSocket: " + JsonUtility.ToJson(_tcpSocket)); |
20 | 34 |
|
21 |
| - _tcpSocket.OnConnect((res) => { |
22 |
| - Debug.Log("onConnect: " + JsonUtility.ToJson(res)); |
23 |
| - }); |
| 35 | + _tcpSocket.OnMessage((res) => { |
| 36 | + Debug.Log("onMessage: " + JsonUtility.ToJson(res)); |
| 37 | + }); |
24 | 38 |
|
25 |
| - _tcpSocket.OnError((res) => { |
26 |
| - Debug.Log("onError: " + JsonUtility.ToJson(res)); |
27 |
| - }); |
| 39 | + _tcpSocket.OnConnect((res) => { |
| 40 | + Debug.Log("onConnect: " + JsonUtility.ToJson(res)); |
| 41 | + }); |
28 | 42 |
|
29 |
| - _tcpSocket.OnClose((res) => { |
30 |
| - Debug.Log("onClose: " + JsonUtility.ToJson(res)); |
31 |
| - }); |
| 43 | + _tcpSocket.OnError((res) => { |
| 44 | + Debug.Log("onError: " + JsonUtility.ToJson(res)); |
| 45 | + }); |
32 | 46 |
|
33 |
| - _tcpSocket.OnMessage((res) => { |
34 |
| - Debug.Log("onMessage: " + JsonUtility.ToJson(res)); |
35 |
| - }); |
| 47 | + _tcpSocket.OnClose((res) => { |
| 48 | + Debug.Log("onClose: " + JsonUtility.ToJson(res)); |
| 49 | + }); |
| 50 | + } else |
| 51 | + { |
| 52 | + Debug.Log("tcp实例已初始化"); |
| 53 | + } |
| 54 | + |
36 | 55 | }
|
37 | 56 |
|
| 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 | + |
| 69 | + } |
| 70 | + |
| 71 | + |
38 | 72 | private void connect() {
|
39 |
| - _tcpSocket.Connect(new TCPSocketConnectOption() { |
40 |
| - address = "192.168.193.2", |
41 |
| - port = 8848 |
42 |
| - }); |
| 73 | + if (_tcpSocket != null && !_connected) { |
| 74 | + Debug.Log("connect test start"); |
| 75 | + _tcpSocket.Connect(new TCPSocketConnectOption() |
| 76 | + { |
| 77 | + address = "www.oooceanworld.com", |
| 78 | + port = 8101 |
| 79 | + }); |
| 80 | + _connected = true; |
| 81 | + } else |
| 82 | + { |
| 83 | + Debug.Log("连接失败:tcp实例未初始化或已连接"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void write() { |
| 88 | + if (_tcpSocket != null && _connected) |
| 89 | + { |
| 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); |
| 95 | + } else |
| 96 | + { |
| 97 | + Debug.Log("发送失败:tcp实例未初始化或未连接"); |
| 98 | + } |
| 99 | + |
43 | 100 | }
|
44 | 101 |
|
45 |
| - private void close() { |
46 |
| - _tcpSocket.Close(); |
| 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 | + } |
47 | 117 | }
|
48 | 118 | }
|
49 | 119 |
|
0 commit comments