Index: sdk/lib/io/websocket_impl.dart |
diff --git a/sdk/lib/io/websocket_impl.dart b/sdk/lib/io/websocket_impl.dart |
index 6e308094aabceb22ef8580c0e658a8cecdc649c4..265760690923f502512435c8e6d8df3d1fada22a 100644 |
--- a/sdk/lib/io/websocket_impl.dart |
+++ b/sdk/lib/io/websocket_impl.dart |
@@ -959,16 +959,26 @@ class _WebSocketImpl extends Stream with _ServiceObject implements WebSocket { |
_outCloseCode = code; |
_outCloseReason = reason; |
} |
- if (_closeTimer == null && !_controller.isClosed) { |
- // When closing the web-socket, we no longer accept data. |
- _closeTimer = new Timer(const Duration(seconds: 5), () { |
- // Reuse code and reason from the local close. |
- _closeCode = _outCloseCode; |
- _closeReason = _outCloseReason; |
- _subscription.cancel(); |
- _controller.close(); |
- _webSockets.remove(_serviceId); |
- }); |
+ if (!_controller.isClosed) { |
+ // If a close has not yet been received from the other end then |
+ // 1) make sure to listen on the stream so the close frame will be |
+ // processed if received. |
+ // 2) set a timer terminate the connection if a close frame is |
+ // not received. |
+ if (!_controller.hasListener) { |
+ _controller.stream.drain().catchError((_) => {}); |
+ } |
+ if (_closeTimer == null) { |
+ // When closing the web-socket, we no longer accept data. |
+ _closeTimer = new Timer(const Duration(seconds: 5), () { |
+ // Reuse code and reason from the local close. |
+ _closeCode = _outCloseCode; |
+ _closeReason = _outCloseReason; |
+ _subscription.cancel(); |
+ _controller.close(); |
+ _webSockets.remove(_serviceId); |
+ }); |
+ } |
} |
return _sink.close(); |
} |