Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: dart/samples/dartiverse_search/web/client.dart

Issue 60293003: Version 0.8.10.5 (Closed) Base URL: http://dart.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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';
6 import 'dart:convert';
5 import 'dart:html'; 7 import 'dart:html';
6 import 'dart:convert';
7 8
8 9
9 class Client { 10 class Client {
10 static const Duration CONNECT_DELAY = const Duration(milliseconds: 500); 11 static const Duration RECONNECT_DELAY = const Duration(milliseconds: 500);
11 12
12 WebSocket webSocket; 13 WebSocket webSocket;
13 final DivElement log = new DivElement(); 14 final DivElement log = new DivElement();
14 SearchInputElement searchElement = querySelector('#q'); 15 SearchInputElement searchElement = querySelector('#q');
15 DivElement statusElement = querySelector('#status'); 16 DivElement statusElement = querySelector('#status');
16 DivElement resultsElement = querySelector('#results'); 17 DivElement resultsElement = querySelector('#results');
17 18
18 Client() { 19 Client() {
19 onDisconnected();
20 searchElement.onChange.listen((e) { 20 searchElement.onChange.listen((e) {
21 search(searchElement.value); 21 search(searchElement.value);
22 searchElement.value = ''; 22 searchElement.value = '';
23 }); 23 });
24 connect();
24 } 25 }
25 26
26 void onConnected() { 27 void connect() {
27 setStatus('');
28 searchElement.disabled = false;
29 searchElement.focus();
30 webSocket.onMessage.listen((e) {
31 onMessage(e.data);
32 });
33 }
34
35 void onDisconnected() {
36 searchElement.disabled = true;
37 setStatus('Disconnected');
38 webSocket = new WebSocket('ws://${Uri.base.host}:${Uri.base.port}/ws'); 28 webSocket = new WebSocket('ws://${Uri.base.host}:${Uri.base.port}/ws');
39 webSocket.onOpen.first.then((_) { 29 webSocket.onOpen.first.then((_) {
40 onConnected(); 30 onConnected();
41 webSocket.onClose.first.then((_) { 31 webSocket.onClose.first.then((_) {
42 print("Connection disconnected to ${webSocket.url}"); 32 print("Connection disconnected to ${webSocket.url}");
43 onDisconnected(); 33 onDisconnected();
44 }); 34 });
45 }); 35 });
46 webSocket.onError.first.then((_) { 36 webSocket.onError.first.then((_) {
47 print("Failed to connect to ${webSocket.url}"); 37 print("Failed to connect to ${webSocket.url}. "
38 "Please run bin/server.dart and try again.");
48 onDisconnected(); 39 onDisconnected();
49 }); 40 });
50 } 41 }
51 42
43 void onConnected() {
44 setStatus('');
45 searchElement.disabled = false;
46 searchElement.focus();
47 webSocket.onMessage.listen((e) {
48 onMessage(e.data);
49 });
50 }
51
52 void onDisconnected() {
53 searchElement.disabled = true;
54 setStatus('Disconnected - start \'bin/server.dart\' to continue');
55 new Timer(RECONNECT_DELAY, connect);
56 }
57
52 void setStatus(String status) { 58 void setStatus(String status) {
53 statusElement.innerHtml = status; 59 statusElement.innerHtml = status;
54 } 60 }
55 61
56 62
57 void onMessage(data) { 63 void onMessage(data) {
58 var json = JSON.decode(data); 64 var json = JSON.decode(data);
59 var response = json['response']; 65 var response = json['response'];
60 switch (response) { 66 switch (response) {
61 case 'searchResult': 67 case 'searchResult':
(...skipping 29 matching lines...) Expand all
91 'input': input 97 'input': input
92 }; 98 };
93 webSocket.send(JSON.encode(request)); 99 webSocket.send(JSON.encode(request));
94 } 100 }
95 } 101 }
96 102
97 103
98 void main() { 104 void main() {
99 var client = new Client(); 105 var client = new Client();
100 } 106 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698