6
6
public class TCPSocket : Details
7
7
{
8
8
private WXTCPSocket _tcpSocket ;
9
+ private bool _connected = false ;
9
10
10
11
// 数据
11
12
private string _stringData1 = "String Data" ;
@@ -16,73 +17,104 @@ public class TCPSocket : Details
16
17
private byte [ ] _bufferData2 = { 0xab , 0x05 , 0xd7 , 0x05 } ;
17
18
private byte [ ] _bufferData3 = new byte [ 8 ] ;
18
19
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
-
24
20
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 ) ;
29
25
}
30
26
31
27
// 测试 API
32
28
protected override void TestAPI ( string [ ] args )
33
29
{
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 ) ) ;
36
34
37
- _tcpSocket . OnMessage ( ( res ) => {
38
- Debug . Log ( "onMessage: " + JsonUtility . ToJson ( res ) ) ;
39
- } ) ;
35
+ _tcpSocket . OnMessage ( ( res ) => {
36
+ Debug . Log ( "onMessage: " + JsonUtility . ToJson ( res ) ) ;
37
+ } ) ;
40
38
41
- _tcpSocket . OnConnect ( ( res ) => {
42
- Debug . Log ( "onConnect: " + JsonUtility . ToJson ( res ) ) ;
43
- } ) ;
39
+ _tcpSocket . OnConnect ( ( res ) => {
40
+ Debug . Log ( "onConnect: " + JsonUtility . ToJson ( res ) ) ;
41
+ } ) ;
44
42
45
- _tcpSocket . OnError ( ( res ) => {
46
- Debug . Log ( "onError: " + JsonUtility . ToJson ( res ) ) ;
47
- } ) ;
43
+ _tcpSocket . OnError ( ( res ) => {
44
+ Debug . Log ( "onError: " + JsonUtility . ToJson ( res ) ) ;
45
+ } ) ;
48
46
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
+
52
55
}
53
56
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
+
59
69
}
60
70
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
+ }
64
86
}
65
87
66
88
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
+
69
101
}
70
102
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
+ }
86
118
}
87
119
}
88
120
0 commit comments