OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of dart_controller_service_isolate; | 5 part of dart_controller_service_isolate; |
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 17 matching lines...) Expand all Loading... |
28 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.'); |
29 return; | 29 return; |
30 } | 30 } |
31 var serial = map['id']; | 31 var serial = map['id']; |
32 onMessage(serial, new Message.fromJsonRpc(map)); | 32 onMessage(serial, new Message.fromJsonRpc(map)); |
33 } else { | 33 } else { |
34 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); | 34 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 post(var serial, dynamic result) { | 38 post(dynamic result) { |
39 try { | 39 try { |
40 if (serial == null && result is! String) { | 40 socket.add(result); |
41 socket.add(result); | |
42 } else { | |
43 Map map = { | |
44 'id': serial, | |
45 'result': result | |
46 }; | |
47 socket.add(JSON.encode(map)); | |
48 } | |
49 } catch (_) { | 41 } catch (_) { |
50 print("Ignoring error posting over WebSocket."); | 42 print("Ignoring error posting over WebSocket."); |
51 } | 43 } |
52 } | 44 } |
53 | 45 |
54 dynamic toJson() { | 46 dynamic toJson() { |
55 Map map = super.toJson(); | 47 Map map = super.toJson(); |
56 map['type'] = 'WebSocketClient'; | 48 map['type'] = 'WebSocketClient'; |
57 map['socket'] = '$socket'; | 49 map['socket'] = '$socket'; |
58 return map; | 50 return map; |
59 } | 51 } |
60 } | 52 } |
61 | 53 |
62 | 54 |
63 class HttpRequestClient extends Client { | 55 class HttpRequestClient extends Client { |
64 static ContentType jsonContentType = | 56 static ContentType jsonContentType = |
65 new ContentType("application", "json", charset: "utf-8"); | 57 new ContentType("application", "json", charset: "utf-8"); |
66 final HttpRequest request; | 58 final HttpRequest request; |
67 | 59 |
68 HttpRequestClient(this.request, VMService service) : super(service); | 60 HttpRequestClient(this.request, VMService service) : super(service); |
69 | 61 |
70 post(var serial, String result) { | 62 post(String result) { |
71 request.response..headers.contentType = jsonContentType | 63 request.response..headers.contentType = jsonContentType |
72 ..write(result) | 64 ..write(result) |
73 ..close(); | 65 ..close(); |
74 close(); | 66 close(); |
75 } | 67 } |
76 | 68 |
77 dynamic toJson() { | 69 dynamic toJson() { |
78 Map map = super.toJson(); | 70 Map map = super.toJson(); |
79 map['type'] = 'HttpRequestClient'; | 71 map['type'] = 'HttpRequestClient'; |
80 map['request'] = '$request'; | 72 map['request'] = '$request'; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 216 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
225 _notifyServerState("", 0); | 217 _notifyServerState("", 0); |
226 return this; | 218 return this; |
227 }); | 219 }); |
228 } | 220 } |
229 | 221 |
230 } | 222 } |
231 | 223 |
232 _notifyServerState(String ip, int port) | 224 _notifyServerState(String ip, int port) |
233 native "ServiceIsolate_NotifyServerState"; | 225 native "ServiceIsolate_NotifyServerState"; |
OLD | NEW |