@@ -41,14 +41,14 @@ private void OnGUI()
4141 socket . OnMessage += Socket_OnMessage ;
4242 socket . OnClose += Socket_OnClose ;
4343 socket . OnError += Socket_OnError ;
44- AddLog ( string . Format ( "Connecting...\n " ) ) ;
44+ AddLog ( string . Format ( "Connecting..." ) ) ;
4545 socket . ConnectAsync ( ) ;
4646 }
4747
4848 GUI . enabled = state == WebSocketState . Open ;
4949 if ( GUILayout . Button ( state == WebSocketState . Closing ? "Closing..." : "Close" ) )
5050 {
51- AddLog ( string . Format ( "Closing...\n " ) ) ;
51+ AddLog ( string . Format ( "Closing..." ) ) ;
5252 socket . CloseAsync ( ) ;
5353 }
5454 GUILayout . EndHorizontal ( ) ;
@@ -57,58 +57,41 @@ private void OnGUI()
5757 sendText = GUILayout . TextArea ( sendText , GUILayout . MinHeight ( 50 ) , width ) ;
5858
5959 GUILayout . BeginHorizontal ( ) ;
60- if ( GUILayout . Button ( "Send" ) )
60+ if ( GUILayout . Button ( "Send" ) && ! string . IsNullOrEmpty ( sendText ) )
6161 {
62- if ( ! string . IsNullOrEmpty ( sendText ) )
63- {
64- socket . SendAsync ( sendText ) ;
65- if ( logMessage )
66- AddLog ( string . Format ( "Send: {0}\n " , sendText ) ) ;
67- sendCount += 1 ;
68- }
62+ socket . SendAsync ( sendText ) ;
63+ AddLog ( string . Format ( "Send: {0}" , sendText ) ) ;
64+ sendCount += 1 ;
6965 }
70- if ( GUILayout . Button ( "Send Bytes" ) )
66+ if ( GUILayout . Button ( "Send Bytes" ) && ! string . IsNullOrEmpty ( sendText ) )
7167 {
72- if ( ! string . IsNullOrEmpty ( sendText ) )
73- {
74- var bytes = System . Text . Encoding . UTF8 . GetBytes ( sendText ) ;
75- socket . SendAsync ( bytes ) ;
76-
77- if ( logMessage )
78- AddLog ( string . Format ( "Send Bytes ({1}): {0}\n " , sendText , bytes . Length ) ) ;
79- sendCount += 1 ;
80- }
68+ var bytes = System . Text . Encoding . UTF8 . GetBytes ( sendText ) ;
69+ socket . SendAsync ( bytes ) ;
70+ AddLog ( string . Format ( "Send Bytes ({1}): {0}" , sendText , bytes . Length ) ) ;
71+ sendCount += 1 ;
8172 }
82- if ( GUILayout . Button ( "Send x100" ) )
73+ if ( GUILayout . Button ( "Send x100" ) && ! string . IsNullOrEmpty ( sendText ) )
8374 {
84- if ( ! string . IsNullOrEmpty ( sendText ) )
75+ for ( int i = 0 ; i < 100 ; i ++ )
8576 {
86- for ( int i = 0 ; i < 100 ; i ++ )
87- {
88- var text = ( i + 1 ) . ToString ( ) + ". " + sendText ;
89- socket . SendAsync ( text ) ;
90-
91- if ( logMessage )
92- AddLog ( string . Format ( "Send: {0}\n " , text ) ) ;
93- sendCount += 1 ;
94- }
77+ var text = ( i + 1 ) . ToString ( ) + ". " + sendText ;
78+ socket . SendAsync ( text ) ;
79+ AddLog ( string . Format ( "Send: {0}" , text ) ) ;
80+ sendCount += 1 ;
9581 }
9682 }
97- if ( GUILayout . Button ( "Send Bytes x100" ) )
83+ if ( GUILayout . Button ( "Send Bytes x100" ) && ! string . IsNullOrEmpty ( sendText ) )
9884 {
99- if ( ! string . IsNullOrEmpty ( sendText ) )
85+ for ( int i = 0 ; i < 100 ; i ++ )
10086 {
101- for ( int i = 0 ; i < 100 ; i ++ )
102- {
103- var text = ( i + 1 ) . ToString ( ) + ". " + sendText ;
104- var bytes = System . Text . Encoding . UTF8 . GetBytes ( text ) ;
105- socket . SendAsync ( bytes ) ;
106- if ( logMessage )
107- AddLog ( string . Format ( "Send Bytes ({1}): {0}\n " , text , bytes . Length ) ) ;
108- sendCount += 1 ;
109- }
87+ var text = ( i + 1 ) . ToString ( ) + ". " + sendText ;
88+ var bytes = System . Text . Encoding . UTF8 . GetBytes ( text ) ;
89+ socket . SendAsync ( bytes ) ;
90+ AddLog ( string . Format ( "Send Bytes ({1}): {0}" , text , bytes . Length ) ) ;
91+ sendCount += 1 ;
11092 }
11193 }
94+
11295 GUILayout . EndHorizontal ( ) ;
11396
11497 GUI . enabled = true ;
@@ -132,42 +115,41 @@ private void OnGUI()
132115
133116 private void AddLog ( string str )
134117 {
135- log += str ;
136- // max log
137- if ( log . Length > 32 * 1024 )
118+ if ( ! logMessage ) return ;
119+ log += str + " \n " ;
120+ if ( log . Length > 4 * 1024 )
138121 {
139- log = log . Substring ( 16 * 1024 ) ;
122+ log = log . Substring ( 2 * 1024 ) ;
140123 }
124+ scrollPos . y = 10000 ;
141125 }
142126
143127 private void Socket_OnOpen ( object sender , OpenEventArgs e )
144128 {
145- AddLog ( string . Format ( "Connected: {0}\n " , address ) ) ;
129+ AddLog ( string . Format ( "Connected: {0}" , address ) ) ;
146130 }
147131
148132 private void Socket_OnMessage ( object sender , MessageEventArgs e )
149133 {
150134 if ( e . IsBinary )
151135 {
152- if ( logMessage )
153- AddLog ( string . Format ( "Receive Bytes ({1}): {0}\n " , e . Data , e . RawData . Length ) ) ;
136+ AddLog ( string . Format ( "Receive Bytes ({1}): {0}" , e . Data , e . RawData . Length ) ) ;
154137 }
155138 else if ( e . IsText )
156139 {
157- if ( logMessage )
158- AddLog ( string . Format ( "Receive: {0}\n " , e . Data ) ) ;
140+ AddLog ( string . Format ( "Receive: {0}" , e . Data ) ) ;
159141 }
160142 receiveCount += 1 ;
161143 }
162144
163145 private void Socket_OnClose ( object sender , CloseEventArgs e )
164146 {
165- AddLog ( string . Format ( "Closed: StatusCode: {0}, Reason: {1}\n " , e . StatusCode , e . Reason ) ) ;
147+ AddLog ( string . Format ( "Closed: StatusCode: {0}, Reason: {1}" , e . StatusCode , e . Reason ) ) ;
166148 }
167149
168150 private void Socket_OnError ( object sender , ErrorEventArgs e )
169151 {
170- AddLog ( string . Format ( "Error: {0}\n " , e . Message ) ) ;
152+ AddLog ( string . Format ( "Error: {0}" , e . Message ) ) ;
171153 }
172154
173155 private void OnApplicationQuit ( )
0 commit comments