| 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; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 try { | 23 try { |
| 24 map = JSON.decode(message); | 24 map = JSON.decode(message); |
| 25 } catch (e) { | 25 } catch (e) { |
| 26 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); | 26 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); |
| 27 return; | 27 return; |
| 28 } | 28 } |
| 29 if (map is! Map) { | 29 if (map is! Map) { |
| 30 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); | 30 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); |
| 31 return; | 31 return; |
| 32 } | 32 } |
| 33 var seq = map['seq']; | 33 var serial = map['id']; |
| 34 onMessage(seq, new Message.fromUri(Uri.parse(map['request']))); | 34 |
| 35 // If the map contains 'params' then this is a JsonRPC message. |
| 36 // We are currently in the process of switching the vmservice over to |
| 37 // JsonRPC. |
| 38 if (map['params'] != null) { |
| 39 onMessage(serial, new Message.fromJsonRpc(map['method'], |
| 40 map['params'])); |
| 41 } else { |
| 42 onMessage(serial, new Message.fromUri(Uri.parse(map['method']))); |
| 43 } |
| 35 } else { | 44 } else { |
| 36 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); | 45 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); |
| 37 } | 46 } |
| 38 } | 47 } |
| 39 | 48 |
| 40 void post(var seq, dynamic response) { | 49 void post(var serial, dynamic response) { |
| 41 try { | 50 try { |
| 42 Map map = { | 51 Map map = { |
| 43 'seq': seq, | 52 'id': serial, |
| 44 'response': response | 53 'response': response |
| 45 }; | 54 }; |
| 46 if (seq == null && response is! String) { | 55 if (serial == null && response is! String) { |
| 47 socket.add(response); | 56 socket.add(response); |
| 48 } else { | 57 } else { |
| 49 socket.add(JSON.encode(map)); | 58 socket.add(JSON.encode(map)); |
| 50 } | 59 } |
| 51 } catch (_) { | 60 } catch (_) { |
| 52 print("Ignoring error posting over WebSocket."); | 61 print("Ignoring error posting over WebSocket."); |
| 53 } | 62 } |
| 54 } | 63 } |
| 55 | 64 |
| 56 dynamic toJson() { | 65 dynamic toJson() { |
| 57 Map map = super.toJson(); | 66 Map map = super.toJson(); |
| 58 map['type'] = 'WebSocketClient'; | 67 map['type'] = 'WebSocketClient'; |
| 59 map['socket'] = '$socket'; | 68 map['socket'] = '$socket'; |
| 60 return map; | 69 return map; |
| 61 } | 70 } |
| 62 } | 71 } |
| 63 | 72 |
| 64 | 73 |
| 65 class HttpRequestClient extends Client { | 74 class HttpRequestClient extends Client { |
| 66 static ContentType jsonContentType = | 75 static ContentType jsonContentType = |
| 67 new ContentType("application", "json", charset: "utf-8"); | 76 new ContentType("application", "json", charset: "utf-8"); |
| 68 final HttpRequest request; | 77 final HttpRequest request; |
| 69 | 78 |
| 70 HttpRequestClient(this.request, service) : super(service); | 79 HttpRequestClient(this.request, service) : super(service); |
| 71 | 80 |
| 72 void post(var seq, String response) { | 81 void post(var serial, String response) { |
| 73 request.response..headers.contentType = jsonContentType | 82 request.response..headers.contentType = jsonContentType |
| 74 ..write(response) | 83 ..write(response) |
| 75 ..close(); | 84 ..close(); |
| 76 close(); | 85 close(); |
| 77 } | 86 } |
| 78 | 87 |
| 79 dynamic toJson() { | 88 dynamic toJson() { |
| 80 Map map = super.toJson(); | 89 Map map = super.toJson(); |
| 81 map['type'] = 'HttpRequestClient'; | 90 map['type'] = 'HttpRequestClient'; |
| 82 map['request'] = '$request'; | 91 map['request'] = '$request'; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 213 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
| 205 _notifyServerState("", 0); | 214 _notifyServerState("", 0); |
| 206 return this; | 215 return this; |
| 207 }); | 216 }); |
| 208 } | 217 } |
| 209 | 218 |
| 210 } | 219 } |
| 211 | 220 |
| 212 void _notifyServerState(String ip, int port) | 221 void _notifyServerState(String ip, int port) |
| 213 native "VMServiceIO_NotifyServerState"; | 222 native "VMServiceIO_NotifyServerState"; |
| OLD | NEW |