|
| 1 | +// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | + |
| 8 | +import 'package:chat_example/client/client_connection.dart'; |
| 9 | +import 'package:chat_example/client/display.dart'; |
| 10 | +import 'package:chat_example/data_model/data_model.dart'; |
| 11 | +import 'package:chat_example/data_model/serializers.dart'; |
| 12 | + |
| 13 | +typedef CommandRunner(String command); |
| 14 | + |
| 15 | +/// Client-side logic for built_json chat example. |
| 16 | +class Client { |
| 17 | + final Stream<String> _keyboardInput; |
| 18 | + final Display _display; |
| 19 | + final ClientConnection _connection; |
| 20 | + |
| 21 | + /// A chat client needs a keyboard, a display and a connection. |
| 22 | + Client(this._keyboardInput, this._display, this._connection) { |
| 23 | + _keyboardInput.listen(_runLocalCommand); |
| 24 | + _connection.dataFromServer.listen(_handleServerData); |
| 25 | + } |
| 26 | + |
| 27 | + void _handleServerData(String data) { |
| 28 | + final response = serializers.deserialize(JSON.decode(data)); |
| 29 | + |
| 30 | + if (response is Response) { |
| 31 | + _display.add(response.render()); |
| 32 | + } else { |
| 33 | + throw new StateError('Invalid data from server: $response'); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + void _runLocalCommand(String command) { |
| 38 | + final handlers = <String, CommandRunner>{ |
| 39 | + '/away': _runAway, |
| 40 | + '/help': _runHelp, |
| 41 | + '/list': _runList, |
| 42 | + '/login': _runLogin, |
| 43 | + '/quit': _runQuit, |
| 44 | + '/status': _runStatus, |
| 45 | + '/tell': _runTell, |
| 46 | + }; |
| 47 | + |
| 48 | + var found = false; |
| 49 | + handlers.forEach((prefix, commandRunner) { |
| 50 | + if (command.startsWith(prefix)) { |
| 51 | + commandRunner(command); |
| 52 | + found = true; |
| 53 | + } |
| 54 | + }); |
| 55 | + if (found) return; |
| 56 | + |
| 57 | + if (command.startsWith('/')) { |
| 58 | + _display.addLocal(command); |
| 59 | + _display.add('Unknown command.'); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + _send(new Chat((b) => b..text = command)); |
| 64 | + } |
| 65 | + |
| 66 | + void _runAway(String command) { |
| 67 | + _send(new Status((b) => b |
| 68 | + ..message = command.substring('/away '.length) |
| 69 | + ..type = StatusType.away)); |
| 70 | + } |
| 71 | + |
| 72 | + void _runHelp(String command) { |
| 73 | + _display.add('''Commands: |
| 74 | +
|
| 75 | +/away <message> -- sets away message |
| 76 | +/help -- for help |
| 77 | +/list -- list online users |
| 78 | +/login <username> <password> -- log in or create new user |
| 79 | +/quit <message> -- quits with message |
| 80 | +/status <message> -- sets status message |
| 81 | +/tell <username> <message> -- private message |
| 82 | +'''); |
| 83 | + } |
| 84 | + |
| 85 | + void _runList(String command) { |
| 86 | + _display.addLocal(command); |
| 87 | + _send(new ListUsers( |
| 88 | + (b) => b..statusTypes.replace([StatusType.online, StatusType.away]))); |
| 89 | + } |
| 90 | + |
| 91 | + void _runLogin(String command) { |
| 92 | + final words = command.split(' '); |
| 93 | + _display.addLocal('/login ${words[1]} ********'); |
| 94 | + _send(new Login((b) => b |
| 95 | + ..username = words[1] |
| 96 | + ..password = words[2])); |
| 97 | + } |
| 98 | + |
| 99 | + void _runQuit(String command) { |
| 100 | + _display.addLocal(command); |
| 101 | + _send(new Status((b) => b |
| 102 | + ..message = command.substring('/quit '.length) |
| 103 | + ..type = StatusType.offline)); |
| 104 | + } |
| 105 | + |
| 106 | + void _runStatus(String command) { |
| 107 | + _display.addLocal(command); |
| 108 | + _display.addLocal(command); |
| 109 | + _send(new Status((b) => b |
| 110 | + ..message = command.substring('/status '.length) |
| 111 | + ..type = StatusType.online)); |
| 112 | + } |
| 113 | + |
| 114 | + void _runTell(String command) { |
| 115 | + _display.addLocal(command); |
| 116 | + final words = command.split(' '); |
| 117 | + final targets = words[1].split(','); |
| 118 | + _send(new Chat((b) => b |
| 119 | + ..text = words.sublist(2).join(' ') |
| 120 | + ..targets.replace(targets))); |
| 121 | + } |
| 122 | + |
| 123 | + void _send(Command command) { |
| 124 | + _connection.sendToServer(JSON.encode(serializers.serialize(command))); |
| 125 | + } |
| 126 | +} |
0 commit comments