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 final WebSocket socket; | 11 final WebSocket socket; |
12 | 12 |
13 WebSocketClient(this.socket, service) : super(service) { | 13 WebSocketClient(this.socket, VMService service) : super(service) { |
14 socket.listen((message) => onWebSocketMessage(message)); | 14 socket.listen((message) => onWebSocketMessage(message)); |
15 socket.done.then((_) => close()); | 15 socket.done.then((_) => close()); |
16 service.subscribe('debug', this); | |
17 service.subscribe('gc', this); | |
18 } | 16 } |
19 | 17 |
20 void onWebSocketMessage(message) { | 18 void onWebSocketMessage(message) { |
21 if (message is String) { | 19 if (message is String) { |
22 var map; | 20 var map; |
23 try { | 21 try { |
24 map = JSON.decode(message); | 22 map = JSON.decode(message); |
25 } catch (e) { | 23 } catch (e) { |
26 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); | 24 socket.close(PARSE_ERROR_CODE, 'Message parse error: $e'); |
27 return; | 25 return; |
28 } | 26 } |
29 if (map is! Map) { | 27 if (map is! Map) { |
30 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); | 28 socket.close(NOT_MAP_ERROR_CODE, 'Message must be a JSON map.'); |
31 return; | 29 return; |
32 } | 30 } |
33 var serial = map['id']; | 31 var serial = map['id']; |
34 onMessage(serial, new Message.fromJsonRpc(map)); | 32 onMessage(serial, new Message.fromJsonRpc(map)); |
35 } else { | 33 } else { |
36 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); | 34 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); |
37 } | 35 } |
38 } | 36 } |
39 | 37 |
40 void post(var serial, dynamic response) { | 38 void post(var serial, dynamic result) { |
41 try { | 39 try { |
42 Map map = { | 40 if (serial == null && result is! String) { |
43 'id': serial, | 41 socket.add(result); |
44 'response': response | |
45 }; | |
46 if (serial == null && response is! String) { | |
47 socket.add(response); | |
48 } else { | 42 } else { |
| 43 Map map = { |
| 44 'id': serial, |
| 45 'result': result |
| 46 }; |
49 socket.add(JSON.encode(map)); | 47 socket.add(JSON.encode(map)); |
50 } | 48 } |
51 } catch (_) { | 49 } catch (_) { |
52 print("Ignoring error posting over WebSocket."); | 50 print("Ignoring error posting over WebSocket."); |
53 } | 51 } |
54 } | 52 } |
55 | 53 |
56 dynamic toJson() { | 54 dynamic toJson() { |
57 Map map = super.toJson(); | 55 Map map = super.toJson(); |
58 map['type'] = 'WebSocketClient'; | 56 map['type'] = 'WebSocketClient'; |
59 map['socket'] = '$socket'; | 57 map['socket'] = '$socket'; |
60 return map; | 58 return map; |
61 } | 59 } |
62 } | 60 } |
63 | 61 |
64 | 62 |
65 class HttpRequestClient extends Client { | 63 class HttpRequestClient extends Client { |
66 static ContentType jsonContentType = | 64 static ContentType jsonContentType = |
67 new ContentType("application", "json", charset: "utf-8"); | 65 new ContentType("application", "json", charset: "utf-8"); |
68 final HttpRequest request; | 66 final HttpRequest request; |
69 | 67 |
70 HttpRequestClient(this.request, service) : super(service); | 68 HttpRequestClient(this.request, VMService service) |
| 69 : super(service, sendEvents:false); |
71 | 70 |
72 void post(var serial, String response) { | 71 void post(var serial, String result) { |
73 request.response..headers.contentType = jsonContentType | 72 request.response..headers.contentType = jsonContentType |
74 ..write(response) | 73 ..write(result) |
75 ..close(); | 74 ..close(); |
76 close(); | 75 close(); |
77 } | 76 } |
78 | 77 |
79 dynamic toJson() { | 78 dynamic toJson() { |
80 Map map = super.toJson(); | 79 Map map = super.toJson(); |
81 map['type'] = 'HttpRequestClient'; | 80 map['type'] = 'HttpRequestClient'; |
82 map['request'] = '$request'; | 81 map['request'] = '$request'; |
83 return map; | 82 return map; |
84 } | 83 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 210 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
212 _notifyServerState("", 0); | 211 _notifyServerState("", 0); |
213 return this; | 212 return this; |
214 }); | 213 }); |
215 } | 214 } |
216 | 215 |
217 } | 216 } |
218 | 217 |
219 void _notifyServerState(String ip, int port) | 218 void _notifyServerState(String ip, int port) |
220 native "VMServiceIO_NotifyServerState"; | 219 native "VMServiceIO_NotifyServerState"; |
OLD | NEW |