Index: dart/samples/dartiverse_search/web/client.dart |
=================================================================== |
--- dart/samples/dartiverse_search/web/client.dart (revision 29908) |
+++ dart/samples/dartiverse_search/web/client.dart (working copy) |
@@ -2,12 +2,13 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
+import 'dart:async'; |
+import 'dart:convert'; |
import 'dart:html'; |
-import 'dart:convert'; |
class Client { |
- static const Duration CONNECT_DELAY = const Duration(milliseconds: 500); |
+ static const Duration RECONNECT_DELAY = const Duration(milliseconds: 500); |
WebSocket webSocket; |
final DivElement log = new DivElement(); |
@@ -16,25 +17,14 @@ |
DivElement resultsElement = querySelector('#results'); |
Client() { |
- onDisconnected(); |
searchElement.onChange.listen((e) { |
search(searchElement.value); |
searchElement.value = ''; |
}); |
+ connect(); |
} |
- void onConnected() { |
- setStatus(''); |
- searchElement.disabled = false; |
- searchElement.focus(); |
- webSocket.onMessage.listen((e) { |
- onMessage(e.data); |
- }); |
- } |
- |
- void onDisconnected() { |
- searchElement.disabled = true; |
- setStatus('Disconnected'); |
+ void connect() { |
webSocket = new WebSocket('ws://${Uri.base.host}:${Uri.base.port}/ws'); |
webSocket.onOpen.first.then((_) { |
onConnected(); |
@@ -44,11 +34,27 @@ |
}); |
}); |
webSocket.onError.first.then((_) { |
- print("Failed to connect to ${webSocket.url}"); |
+ print("Failed to connect to ${webSocket.url}. " |
+ "Please run bin/server.dart and try again."); |
onDisconnected(); |
}); |
} |
+ void onConnected() { |
+ setStatus(''); |
+ searchElement.disabled = false; |
+ searchElement.focus(); |
+ webSocket.onMessage.listen((e) { |
+ onMessage(e.data); |
+ }); |
+ } |
+ |
+ void onDisconnected() { |
+ searchElement.disabled = true; |
+ setStatus('Disconnected - start \'bin/server.dart\' to continue'); |
+ new Timer(RECONNECT_DELAY, connect); |
+ } |
+ |
void setStatus(String status) { |
statusElement.innerHtml = status; |
} |