| 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 vmservice_io; | 5 part of vmservice_io; |
| 6 | 6 |
| 7 class WebSocketClient extends Client { | 7 class WebSocketClient extends Client { |
| 8 static const int PARSE_ERROR_CODE = 4000; | 8 static const int PARSE_ERROR_CODE = 4000; |
| 9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; | 9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; |
| 10 static const int NOT_MAP_ERROR_CODE = 4002; | 10 static const int NOT_MAP_ERROR_CODE = 4002; |
| 11 static const int ID_ERROR_CODE = 4003; |
| 11 final WebSocket socket; | 12 final WebSocket socket; |
| 12 | 13 |
| 13 WebSocketClient(this.socket, VMService service) : super(service) { | 14 WebSocketClient(this.socket, VMService service) : super(service) { |
| 14 socket.listen((message) => onWebSocketMessage(message)); | 15 socket.listen((message) => onWebSocketMessage(message)); |
| 15 socket.done.then((_) => close()); | 16 socket.done.then((_) => close()); |
| 16 } | 17 } |
| 17 | 18 |
| 18 void onWebSocketMessage(message) { | 19 void onWebSocketMessage(message) { |
| 19 if (message is String) { | 20 if (message is String) { |
| 20 var map; | 21 var map; |
| 21 try { | 22 try { |
| 22 map = JSON.decode(message); | 23 map = JSON.decode(message); |
| 23 } catch (e) { | 24 } catch (e) { |
| 24 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); | 25 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); |
| 25 return; | 26 return; |
| 26 } | 27 } |
| 27 if (map is! Map) { | 28 if (map is! Map) { |
| 28 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); | 29 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); |
| 29 return; | 30 return; |
| 30 } | 31 } |
| 31 var serial = map['id']; | 32 var serial = map['id']; |
| 33 if (serial != null && serial is! num && serial is! String) { |
| 34 socket.close(ID_ERROR_CODE, '"id" must be a number, string, or null.'); |
| 35 } |
| 32 onMessage(serial, new Message.fromJsonRpc(this, map)); | 36 onMessage(serial, new Message.fromJsonRpc(this, map)); |
| 33 } else { | 37 } else { |
| 34 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); | 38 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); |
| 35 } | 39 } |
| 36 } | 40 } |
| 37 | 41 |
| 38 void post(dynamic result) { | 42 void post(dynamic result) { |
| 39 try { | 43 try { |
| 40 socket.add(result); | 44 socket.add(result); |
| 41 } catch (_) { | 45 } catch (_) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 206 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
| 203 _notifyServerState("", 0); | 207 _notifyServerState("", 0); |
| 204 return this; | 208 return this; |
| 205 }); | 209 }); |
| 206 } | 210 } |
| 207 | 211 |
| 208 } | 212 } |
| 209 | 213 |
| 210 void _notifyServerState(String ip, int port) | 214 void _notifyServerState(String ip, int port) |
| 211 native "VMServiceIO_NotifyServerState"; | 215 native "VMServiceIO_NotifyServerState"; |
| OLD | NEW |