| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 import 'dart:convert'; | |
| 7 import 'dart:html'; | |
| 8 | |
| 9 | |
| 10 class Client { | |
| 11 static const Duration RECONNECT_DELAY = const Duration(milliseconds: 500); | |
| 12 | |
| 13 bool connectPending = false; | |
| 14 String mostRecentSearch = null; | |
| 15 WebSocket webSocket; | |
| 16 final DivElement log = new DivElement(); | |
| 17 SearchInputElement searchElement = querySelector('#q'); | |
| 18 DivElement statusElement = querySelector('#status'); | |
| 19 DivElement resultsElement = querySelector('#results'); | |
| 20 | |
| 21 Client() { | |
| 22 searchElement.onChange.listen((e) { | |
| 23 search(searchElement.value); | |
| 24 searchElement.value = ''; | |
| 25 }); | |
| 26 connect(); | |
| 27 } | |
| 28 | |
| 29 void connect() { | |
| 30 connectPending = false; | |
| 31 webSocket = new WebSocket('ws://${Uri.base.host}:${Uri.base.port}/ws'); | |
| 32 webSocket.onOpen.first.then((_) { | |
| 33 onConnected(); | |
| 34 webSocket.onClose.first.then((_) { | |
| 35 print("Connection disconnected to ${webSocket.url}."); | |
| 36 onDisconnected(); | |
| 37 }); | |
| 38 }); | |
| 39 webSocket.onError.first.then((_) { | |
| 40 print("Failed to connect to ${webSocket.url}. " | |
| 41 "Run bin/server.dart and try again."); | |
| 42 onDisconnected(); | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 void onConnected() { | |
| 47 setStatus(''); | |
| 48 searchElement.disabled = false; | |
| 49 searchElement.focus(); | |
| 50 webSocket.onMessage.listen((e) { | |
| 51 handleMessage(e.data); | |
| 52 }); | |
| 53 } | |
| 54 | |
| 55 void onDisconnected() { | |
| 56 if (connectPending) return; | |
| 57 connectPending = true; | |
| 58 setStatus('Disconnected. Start \'bin/server.dart\' to continue.'); | |
| 59 searchElement.disabled = true; | |
| 60 new Timer(RECONNECT_DELAY, connect); | |
| 61 } | |
| 62 | |
| 63 void setStatus(String status) { | |
| 64 statusElement.innerHtml = status; | |
| 65 } | |
| 66 | |
| 67 | |
| 68 void handleMessage(data) { | |
| 69 var json = JSON.decode(data); | |
| 70 var response = json['response']; | |
| 71 switch (response) { | |
| 72 case 'searchResult': | |
| 73 addResult(json['source'], json['title'], json['link']); | |
| 74 break; | |
| 75 | |
| 76 case 'searchDone': | |
| 77 setStatus(resultsElement.children.isEmpty | |
| 78 ? "$mostRecentSearch: No results found" | |
| 79 : "$mostRecentSearch: " | |
| 80 "${resultsElement.children.length} results found"); | |
| 81 break; | |
| 82 | |
| 83 default: | |
| 84 print("Invalid response: '$response'"); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void addResult(String source, String title, String link) { | |
| 89 var result = new DivElement(); | |
| 90 result.children.add(new HeadingElement.h2()..innerHtml = source); | |
| 91 result.children.add( | |
| 92 new AnchorElement(href: link) | |
| 93 ..innerHtml = title | |
| 94 ..target = '_blank'); | |
| 95 result.classes.add('result'); | |
| 96 resultsElement.children.add(result); | |
| 97 } | |
| 98 | |
| 99 void search(String input) { | |
| 100 if (input.isEmpty) return; | |
| 101 setStatus('Searching for $input...'); | |
| 102 resultsElement.children.clear(); | |
| 103 var request = { | |
| 104 'request': 'search', | |
| 105 'input': input | |
| 106 }; | |
| 107 webSocket.send(JSON.encode(request)); | |
| 108 mostRecentSearch = input; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 | |
| 113 void main() { | |
| 114 var client = new Client(); | |
| 115 } | |
| OLD | NEW |