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