@@ -25,11 +25,9 @@ public class WebSocket : EventTarget, IDisposable
25
25
const Int32 SendChunkSize = 1024 ;
26
26
27
27
readonly Url _url ;
28
- readonly MemoryStream _buffered ;
29
28
readonly CancellationTokenSource _cts ;
30
29
readonly ClientWebSocket _ws ;
31
30
32
- String _protocol ;
33
31
WebSocketReadyState _state ;
34
32
35
33
#endregion
@@ -86,9 +84,7 @@ public event DomEventHandler Closed
86
84
public WebSocket ( String url , params String [ ] protocols )
87
85
{
88
86
_url = new Url ( url ) ;
89
- _protocol = String . Empty ;
90
87
_state = WebSocketReadyState . Connecting ;
91
- _buffered = new MemoryStream ( ) ;
92
88
_cts = new CancellationTokenSource ( ) ;
93
89
94
90
if ( _url . IsInvalid || _url . IsRelative )
@@ -141,7 +137,7 @@ public WebSocketReadyState ReadyState
141
137
[ DomName ( "bufferedAmount" ) ]
142
138
public Int64 Buffered
143
139
{
144
- get { return _buffered . Length ; }
140
+ get { return 0 ; }
145
141
}
146
142
147
143
/// <summary>
@@ -150,7 +146,7 @@ public Int64 Buffered
150
146
[ DomName ( "protocol" ) ]
151
147
public String Protocol
152
148
{
153
- get { return _protocol ; }
149
+ get { return _ws . SubProtocol ?? String . Empty ; }
154
150
}
155
151
156
152
#endregion
@@ -164,7 +160,14 @@ public String Protocol
164
160
[ DomName ( "send" ) ]
165
161
public void Send ( String data )
166
162
{
167
- SendAsync ( data ) . Wait ( ) ;
163
+ if ( _state == WebSocketReadyState . Open )
164
+ {
165
+ SendAsync ( data ) . Forget ( ) ;
166
+ }
167
+ else if ( _state != WebSocketReadyState . Connecting )
168
+ {
169
+ throw new Exception ( "WebSocket is already in CLOSING or CLOSED state." ) ;
170
+ }
168
171
}
169
172
170
173
/// <summary>
@@ -173,9 +176,12 @@ public void Send(String data)
173
176
[ DomName ( "close" ) ]
174
177
public void Close ( )
175
178
{
176
- _state = WebSocketReadyState . Closing ;
177
- StopListen ( ) ;
178
- OnDisconnected ( ) ;
179
+ if ( _state != WebSocketReadyState . Closed && _state != WebSocketReadyState . Closing )
180
+ {
181
+ _state = WebSocketReadyState . Closing ;
182
+ StopListen ( ) ;
183
+ OnDisconnected ( ) ;
184
+ }
179
185
}
180
186
181
187
void IDisposable . Dispose ( )
@@ -200,17 +206,14 @@ static Boolean IsValid(String protocol)
200
206
201
207
async Task SendAsync ( String message )
202
208
{
203
- if ( _ws . State != WebSocketState . Open )
204
- throw new Exception ( "WebSocket is already in CLOSING or CLOSED state." ) ;
205
-
206
209
var messageBuffer = Encoding . UTF8 . GetBytes ( message ) ;
207
210
var remainder = 0 ;
208
211
var messagesCount = Math . DivRem ( messageBuffer . Length , SendChunkSize , out remainder ) ;
209
212
210
213
if ( remainder > 0 )
211
214
messagesCount ++ ;
212
- else
213
- remainder = SendChunkSize ;
215
+
216
+ remainder = messageBuffer . Length ;
214
217
215
218
for ( var i = 0 ; i < messagesCount ; i ++ )
216
219
{
@@ -219,6 +222,7 @@ async Task SendAsync(String message)
219
222
var count = lastMessage ? remainder : SendChunkSize ;
220
223
var segment = new ArraySegment < Byte > ( messageBuffer , offset , count ) ;
221
224
await _ws . SendAsync ( segment , WebSocketMessageType . Text , lastMessage , _cts . Token ) . ConfigureAwait ( false ) ;
225
+ remainder -= SendChunkSize ;
222
226
}
223
227
}
224
228
0 commit comments