OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library service_html; | 5 library service_html; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 | 10 |
11 import 'package:observatory/service_common.dart'; | 11 import 'package:observatory/service_common.dart'; |
12 | 12 |
13 // Export the service library. | 13 // Export the service library. |
14 export 'package:observatory/service_common.dart'; | 14 export 'package:observatory/service_common.dart'; |
15 | 15 |
16 class _HtmlWebSocket implements CommonWebSocket { | 16 class _HtmlWebSocket implements CommonWebSocket { |
17 WebSocket _webSocket; | 17 WebSocket _webSocket; |
18 | 18 |
19 void connect(String address, | 19 void connect(String address, void onOpen(), void onMessage(dynamic data), |
20 void onOpen(), | 20 void onError(), void onClose()) { |
21 void onMessage(dynamic data), | |
22 void onError(), | |
23 void onClose()) { | |
24 _webSocket = new WebSocket(address); | 21 _webSocket = new WebSocket(address); |
25 _webSocket.onClose.listen((CloseEvent) => onClose()); | 22 _webSocket.onClose.listen((CloseEvent) => onClose()); |
26 _webSocket.onError.listen((Event) => onError()); | 23 _webSocket.onError.listen((Event) => onError()); |
27 _webSocket.onOpen.listen((Event) => onOpen()); | 24 _webSocket.onOpen.listen((Event) => onOpen()); |
28 _webSocket.onMessage.listen((MessageEvent event) => onMessage(event.data)); | 25 _webSocket.onMessage.listen((MessageEvent event) => onMessage(event.data)); |
29 } | 26 } |
30 | 27 |
31 bool get isOpen => _webSocket.readyState == WebSocket.OPEN; | 28 bool get isOpen => _webSocket.readyState == WebSocket.OPEN; |
32 | 29 |
33 void send(dynamic data) { | 30 void send(dynamic data) { |
34 _webSocket.send(data); | 31 _webSocket.send(data); |
35 } | 32 } |
36 | 33 |
37 void close() { | 34 void close() { |
38 _webSocket.close(); | 35 _webSocket.close(); |
39 } | 36 } |
40 | 37 |
41 Future<ByteData> nonStringToByteData(dynamic data) { | 38 Future<ByteData> nonStringToByteData(dynamic data) { |
42 assert(data is Blob); | 39 assert(data is Blob); |
43 FileReader fileReader = new FileReader(); | 40 FileReader fileReader = new FileReader(); |
44 fileReader.readAsArrayBuffer(data); | 41 fileReader.readAsArrayBuffer(data); |
45 return fileReader.onLoadEnd.first.then((e) { | 42 return fileReader.onLoadEnd.first.then((e) { |
46 var result = fileReader.result; | 43 var result = fileReader.result; |
47 return new ByteData.view(result.buffer, | 44 return new ByteData.view( |
48 result.offsetInBytes, | 45 result.buffer, result.offsetInBytes, result.length); |
49 result.length); | |
50 }); | 46 }); |
51 } | 47 } |
52 } | 48 } |
53 | 49 |
54 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM | 50 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM |
55 /// can be embedded in Chromium or standalone. In the case of Chromium, we | 51 /// can be embedded in Chromium or standalone. In the case of Chromium, we |
56 /// make the service requests via the Chrome Remote Debugging Protocol. | 52 /// make the service requests via the Chrome Remote Debugging Protocol. |
57 class WebSocketVM extends CommonWebSocketVM { | 53 class WebSocketVM extends CommonWebSocketVM { |
58 WebSocketVM(WebSocketVMTarget target) : super(target, new _HtmlWebSocket()); | 54 WebSocketVM(WebSocketVMTarget target) : super(target, new _HtmlWebSocket()); |
59 } | 55 } |
OLD | NEW |