| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library service_io; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 import 'dart:typed_data'; | |
| 10 | |
| 11 import 'package:logging/logging.dart'; | |
| 12 import 'package:observatory/service_common.dart'; | |
| 13 | |
| 14 // Export the service library. | |
| 15 export 'package:observatory/service_common.dart'; | |
| 16 | |
| 17 class _IOWebSocket implements CommonWebSocket { | |
| 18 WebSocket _webSocket; | |
| 19 | |
| 20 void connect(String address, | |
| 21 void onOpen(), | |
| 22 void onMessage(dynamic data), | |
| 23 void onError(), | |
| 24 void onClose()) { | |
| 25 WebSocket.connect(address).then((WebSocket socket) { | |
| 26 _webSocket = socket; | |
| 27 _webSocket.listen( | |
| 28 onMessage, | |
| 29 onError: (dynamic) => onError(), | |
| 30 onDone: onClose, | |
| 31 cancelOnError: true); | |
| 32 onOpen(); | |
| 33 }).catchError((e, st) { | |
| 34 onError(); | |
| 35 }); | |
| 36 } | |
| 37 | |
| 38 bool get isOpen => | |
| 39 (_webSocket != null) && (_webSocket.readyState == WebSocket.OPEN); | |
| 40 | |
| 41 void send(dynamic data) { | |
| 42 _webSocket.add(data); | |
| 43 } | |
| 44 | |
| 45 void close() { | |
| 46 if (_webSocket != null) { | |
| 47 _webSocket.close(); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 Future<ByteData> nonStringToByteData(dynamic data) { | |
| 52 assert(data is Uint8List); | |
| 53 Logger.root.info('Binary data size in bytes: ${data.lengthInBytes}'); | |
| 54 return new Future.sync(() => | |
| 55 new ByteData.view(data.buffer, | |
| 56 data.offsetInBytes, | |
| 57 data.lengthInBytes)); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM | |
| 62 /// can be embedded in Chromium or standalone. In the case of Chromium, we | |
| 63 /// make the service requests via the Chrome Remote Debugging Protocol. | |
| 64 class WebSocketVM extends CommonWebSocketVM { | |
| 65 WebSocketVM(WebSocketVMTarget target) : super(target, new _IOWebSocket()); | |
| 66 } | |
| OLD | NEW |