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