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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:html'; | 7 import 'dart:html'; |
8 | 8 |
9 | 9 |
10 class Client { | 10 class Client { |
11 static const Duration RECONNECT_DELAY = const Duration(milliseconds: 500); | 11 static const Duration RECONNECT_DELAY = const Duration(milliseconds: 500); |
12 | 12 |
| 13 bool connectPending = false; |
13 WebSocket webSocket; | 14 WebSocket webSocket; |
14 final DivElement log = new DivElement(); | 15 final DivElement log = new DivElement(); |
15 SearchInputElement searchElement = querySelector('#q'); | 16 SearchInputElement searchElement = querySelector('#q'); |
16 DivElement statusElement = querySelector('#status'); | 17 DivElement statusElement = querySelector('#status'); |
17 DivElement resultsElement = querySelector('#results'); | 18 DivElement resultsElement = querySelector('#results'); |
18 | 19 |
19 Client() { | 20 Client() { |
20 searchElement.onChange.listen((e) { | 21 searchElement.onChange.listen((e) { |
21 search(searchElement.value); | 22 search(searchElement.value); |
22 searchElement.value = ''; | 23 searchElement.value = ''; |
23 }); | 24 }); |
24 connect(); | 25 connect(); |
25 } | 26 } |
26 | 27 |
27 void connect() { | 28 void connect() { |
| 29 connectPending = false; |
28 webSocket = new WebSocket('ws://${Uri.base.host}:${Uri.base.port}/ws'); | 30 webSocket = new WebSocket('ws://${Uri.base.host}:${Uri.base.port}/ws'); |
29 webSocket.onOpen.first.then((_) { | 31 webSocket.onOpen.first.then((_) { |
30 onConnected(); | 32 onConnected(); |
31 webSocket.onClose.first.then((_) { | 33 webSocket.onClose.first.then((_) { |
32 print("Connection disconnected to ${webSocket.url}"); | 34 print("Connection disconnected to ${webSocket.url}"); |
33 onDisconnected(); | 35 onDisconnected(); |
34 }); | 36 }); |
35 }); | 37 }); |
36 webSocket.onError.first.then((_) { | 38 webSocket.onError.first.then((_) { |
37 print("Failed to connect to ${webSocket.url}. " | 39 print("Failed to connect to ${webSocket.url}. " |
38 "Please run bin/server.dart and try again."); | 40 "Please run bin/server.dart and try again."); |
39 onDisconnected(); | 41 onDisconnected(); |
40 }); | 42 }); |
41 } | 43 } |
42 | 44 |
43 void onConnected() { | 45 void onConnected() { |
44 setStatus(''); | 46 setStatus(''); |
45 searchElement.disabled = false; | 47 searchElement.disabled = false; |
46 searchElement.focus(); | 48 searchElement.focus(); |
47 webSocket.onMessage.listen((e) { | 49 webSocket.onMessage.listen((e) { |
48 onMessage(e.data); | 50 onMessage(e.data); |
49 }); | 51 }); |
50 } | 52 } |
51 | 53 |
52 void onDisconnected() { | 54 void onDisconnected() { |
| 55 if (connectPending) return; |
| 56 connectPending = true; |
| 57 setStatus('Disconnected - start \'bin/server.dart\' to continue'); |
53 searchElement.disabled = true; | 58 searchElement.disabled = true; |
54 setStatus('Disconnected - start \'bin/server.dart\' to continue'); | |
55 new Timer(RECONNECT_DELAY, connect); | 59 new Timer(RECONNECT_DELAY, connect); |
56 } | 60 } |
57 | 61 |
58 void setStatus(String status) { | 62 void setStatus(String status) { |
59 statusElement.innerHtml = status; | 63 statusElement.innerHtml = status; |
60 } | 64 } |
61 | 65 |
62 | 66 |
63 void onMessage(data) { | 67 void onMessage(data) { |
64 var json = JSON.decode(data); | 68 var json = JSON.decode(data); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 'input': input | 101 'input': input |
98 }; | 102 }; |
99 webSocket.send(JSON.encode(request)); | 103 webSocket.send(JSON.encode(request)); |
100 } | 104 } |
101 } | 105 } |
102 | 106 |
103 | 107 |
104 void main() { | 108 void main() { |
105 var client = new Client(); | 109 var client = new Client(); |
106 } | 110 } |
OLD | NEW |