OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
8 | 8 |
9 // Matches _WebSocketOpcode. | 9 // Matches _WebSocketOpcode. |
10 class _WebSocketMessageType { | 10 class _WebSocketMessageType { |
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 var protocol = response.headers.value('Sec-WebSocket-Protocol'); | 850 var protocol = response.headers.value('Sec-WebSocket-Protocol'); |
851 return response.detachSocket() | 851 return response.detachSocket() |
852 .then((socket) => new _WebSocketImpl._fromSocket(socket, protocol)); | 852 .then((socket) => new _WebSocketImpl._fromSocket(socket, protocol)); |
853 }); | 853 }); |
854 } | 854 } |
855 | 855 |
856 _WebSocketImpl._fromSocket(this._socket, this.protocol, | 856 _WebSocketImpl._fromSocket(this._socket, this.protocol, |
857 [this._serverSide = false]) { | 857 [this._serverSide = false]) { |
858 _consumer = new _WebSocketConsumer(this, _socket); | 858 _consumer = new _WebSocketConsumer(this, _socket); |
859 _sink = new _StreamSinkImpl(_consumer); | 859 _sink = new _StreamSinkImpl(_consumer); |
| 860 _sink.done.catchError((e) { |
| 861 if (!_controller.isClosed) { |
| 862 _close(WebSocketStatus.ABNORMAL_CLOSURE); |
| 863 _controller.addError(e); |
| 864 _controller.close(); |
| 865 } |
| 866 }); |
860 _readyState = WebSocket.OPEN; | 867 _readyState = WebSocket.OPEN; |
861 | 868 |
862 var transformer = new _WebSocketProtocolTransformer(_serverSide); | 869 var transformer = new _WebSocketProtocolTransformer(_serverSide); |
863 _subscription = _socket.transform(transformer).listen( | 870 _subscription = _socket.transform(transformer).listen( |
864 (data) { | 871 (data) { |
865 if (data is _WebSocketPing) { | 872 if (data is _WebSocketPing) { |
866 if (!_writeClosed) _consumer.add(new _WebSocketPong(data.payload)); | 873 if (!_writeClosed) _consumer.add(new _WebSocketPong(data.payload)); |
867 } else if (data is _WebSocketPong) { | 874 } else if (data is _WebSocketPong) { |
868 // Simply set pingInterval, as it'll cancel any timers. | 875 // Simply set pingInterval, as it'll cancel any timers. |
869 pingInterval = _pingInterval; | 876 pingInterval = _pingInterval; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 } | 993 } |
987 return _sink.close(); | 994 return _sink.close(); |
988 } | 995 } |
989 | 996 |
990 void _close([int code, String reason]) { | 997 void _close([int code, String reason]) { |
991 if (_writeClosed) return; | 998 if (_writeClosed) return; |
992 if (_outCloseCode == null) { | 999 if (_outCloseCode == null) { |
993 _outCloseCode = code; | 1000 _outCloseCode = code; |
994 _outCloseReason = reason; | 1001 _outCloseReason = reason; |
995 } | 1002 } |
| 1003 if (_closeCode == null) { |
| 1004 _closeCode = code; |
| 1005 _closeReason = reason; |
| 1006 } |
996 _writeClosed = true; | 1007 _writeClosed = true; |
997 _consumer.closeSocket(); | 1008 _consumer.closeSocket(); |
998 _webSockets.remove(_serviceId); | 1009 _webSockets.remove(_serviceId); |
999 } | 1010 } |
1000 | 1011 |
1001 String get _serviceTypePath => 'io/websockets'; | 1012 String get _serviceTypePath => 'io/websockets'; |
1002 String get _serviceTypeName => 'WebSocket'; | 1013 String get _serviceTypeName => 'WebSocket'; |
1003 | 1014 |
1004 Map _toJSON(bool ref) { | 1015 Map _toJSON(bool ref) { |
1005 var name = '${_socket.address.host}:${_socket.port}'; | 1016 var name = '${_socket.address.host}:${_socket.port}'; |
(...skipping 24 matching lines...) Expand all Loading... |
1030 (code < WebSocketStatus.NORMAL_CLOSURE || | 1041 (code < WebSocketStatus.NORMAL_CLOSURE || |
1031 code == WebSocketStatus.RESERVED_1004 || | 1042 code == WebSocketStatus.RESERVED_1004 || |
1032 code == WebSocketStatus.NO_STATUS_RECEIVED || | 1043 code == WebSocketStatus.NO_STATUS_RECEIVED || |
1033 code == WebSocketStatus.ABNORMAL_CLOSURE || | 1044 code == WebSocketStatus.ABNORMAL_CLOSURE || |
1034 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && | 1045 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && |
1035 code < WebSocketStatus.RESERVED_1015) || | 1046 code < WebSocketStatus.RESERVED_1015) || |
1036 (code >= WebSocketStatus.RESERVED_1015 && | 1047 (code >= WebSocketStatus.RESERVED_1015 && |
1037 code < 3000)); | 1048 code < 3000)); |
1038 } | 1049 } |
1039 } | 1050 } |
OLD | NEW |