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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); | 44 socket.close(BINARY_MESSAGE_ERROR_CODE, 'Message must be a string.'); |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 void post(dynamic result) { | 48 void post(dynamic result) { |
49 if (result == null) { | 49 if (result == null) { |
50 // Do nothing. | 50 // Do nothing. |
51 return; | 51 return; |
52 } | 52 } |
53 try { | 53 try { |
54 socket.add(result); | 54 if (result is String || result is Uint8List) { |
| 55 socket.add(result); // String or binary message. |
| 56 } else { |
| 57 // String message as external Uint8List. |
| 58 assert(result is List); |
| 59 Uint8List cstring = result[0]; |
| 60 // TODO(rmacnak): cstring may be large. Add a way to pass an encoded |
| 61 // string to a web socket that will be sent as a text message to avoid |
| 62 // the space overhead of converting cstring to a Dart string. |
| 63 socket.add(UTF8.decode(cstring)); |
| 64 } |
55 } catch (_) { | 65 } catch (_) { |
56 print("Ignoring error posting over WebSocket."); | 66 print("Ignoring error posting over WebSocket."); |
57 } | 67 } |
58 } | 68 } |
59 | 69 |
60 dynamic toJson() { | 70 dynamic toJson() { |
61 Map map = super.toJson(); | 71 Map map = super.toJson(); |
62 map['type'] = 'WebSocketClient'; | 72 map['type'] = 'WebSocketClient'; |
63 map['socket'] = '$socket'; | 73 map['socket'] = '$socket'; |
64 return map; | 74 return map; |
65 } | 75 } |
66 } | 76 } |
67 | 77 |
68 | 78 |
69 class HttpRequestClient extends Client { | 79 class HttpRequestClient extends Client { |
70 static ContentType jsonContentType = | 80 static ContentType jsonContentType = |
71 new ContentType("application", "json", charset: "utf-8"); | 81 new ContentType("application", "json", charset: "utf-8"); |
72 final HttpRequest request; | 82 final HttpRequest request; |
73 | 83 |
74 HttpRequestClient(this.request, VMService service) | 84 HttpRequestClient(this.request, VMService service) |
75 : super(service, sendEvents:false); | 85 : super(service, sendEvents:false); |
76 | 86 |
77 disconnect() { | 87 disconnect() { |
78 request.response.close(); | 88 request.response.close(); |
79 close(); | 89 close(); |
80 } | 90 } |
81 | 91 |
82 void post(String result) { | 92 void post(dynamic result) { |
83 if (result == null) { | 93 if (result == null) { |
84 close(); | 94 close(); |
85 return; | 95 return; |
86 } | 96 } |
87 request.response..headers.contentType = jsonContentType | 97 HttpResponse response = request.response; |
88 ..write(result) | 98 response.headers.contentType = jsonContentType; |
89 ..close(); | 99 if (result is String) { |
| 100 response.write(result); |
| 101 } else { |
| 102 assert(result is List); |
| 103 Uint8List cstring = result[0]; // Already in UTF-8. |
| 104 response.add(cstring); |
| 105 } |
| 106 response.close(); |
90 close(); | 107 close(); |
91 } | 108 } |
92 | 109 |
93 dynamic toJson() { | 110 dynamic toJson() { |
94 Map map = super.toJson(); | 111 Map map = super.toJson(); |
95 map['type'] = 'HttpRequestClient'; | 112 map['type'] = 'HttpRequestClient'; |
96 map['request'] = '$request'; | 113 map['request'] = '$request'; |
97 return map; | 114 return map; |
98 } | 115 } |
99 } | 116 } |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 _notifyServerState("", 0); | 321 _notifyServerState("", 0); |
305 onServerAddressChange(null); | 322 onServerAddressChange(null); |
306 return this; | 323 return this; |
307 }); | 324 }); |
308 } | 325 } |
309 | 326 |
310 } | 327 } |
311 | 328 |
312 void _notifyServerState(String ip, int port) | 329 void _notifyServerState(String ip, int port) |
313 native "VMServiceIO_NotifyServerState"; | 330 native "VMServiceIO_NotifyServerState"; |
OLD | NEW |