1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
mod echo;
use iced::alignment::{self, Alignment};
use iced::button::{self, Button};
use iced::executor;
use iced::scrollable::{self, Scrollable};
use iced::text_input::{self, TextInput};
use iced::{
Application, Color, Column, Command, Container, Element, Length, Row,
Settings, Subscription, Text,
};
pub fn main() -> iced::Result {
WebSocket::run(Settings::default())
}
#[derive(Default)]
struct WebSocket {
messages: Vec<echo::Message>,
message_log: scrollable::State,
new_message: String,
new_message_state: text_input::State,
new_message_button: button::State,
state: State,
}
#[derive(Debug, Clone)]
enum Message {
NewMessageChanged(String),
Send(echo::Message),
Echo(echo::Event),
Server,
}
impl Application for WebSocket {
type Message = Message;
type Flags = ();
type Executor = executor::Default;
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
(
Self::default(),
Command::perform(echo::server::run(), |_| Message::Server),
)
}
fn title(&self) -> String {
String::from("WebSocket - Iced")
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::NewMessageChanged(new_message) => {
self.new_message = new_message;
}
Message::Send(message) => match &mut self.state {
State::Connected(connection) => {
self.new_message.clear();
connection.send(message);
}
State::Disconnected => {}
},
Message::Echo(event) => match event {
echo::Event::Connected(connection) => {
self.state = State::Connected(connection);
self.messages.push(echo::Message::connected());
}
echo::Event::Disconnected => {
self.state = State::Disconnected;
self.messages.push(echo::Message::disconnected());
}
echo::Event::MessageReceived(message) => {
self.messages.push(message);
self.message_log.snap_to(1.0);
}
},
Message::Server => {}
}
Command::none()
}
fn subscription(&self) -> Subscription<Message> {
echo::connect().map(Message::Echo)
}
fn view(&mut self) -> Element<Message> {
let message_log = if self.messages.is_empty() {
Container::new(
Text::new("Your messages will appear here...")
.color(Color::from_rgb8(0x88, 0x88, 0x88)),
)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
} else {
self.messages
.iter()
.cloned()
.fold(
Scrollable::new(&mut self.message_log),
|scrollable, message| scrollable.push(Text::new(message)),
)
.width(Length::Fill)
.height(Length::Fill)
.spacing(10)
.into()
};
let new_message_input = {
let mut input = TextInput::new(
&mut self.new_message_state,
"Type a message...",
&self.new_message,
Message::NewMessageChanged,
)
.padding(10);
let mut button = Button::new(
&mut self.new_message_button,
Text::new("Send")
.height(Length::Fill)
.vertical_alignment(alignment::Vertical::Center),
)
.padding([0, 20]);
if matches!(self.state, State::Connected(_)) {
if let Some(message) = echo::Message::new(&self.new_message) {
input = input.on_submit(Message::Send(message.clone()));
button = button.on_press(Message::Send(message));
}
}
Row::with_children(vec![input.into(), button.into()])
.spacing(10)
.align_items(Alignment::Fill)
};
Column::with_children(vec![message_log, new_message_input.into()])
.width(Length::Fill)
.height(Length::Fill)
.padding(20)
.spacing(10)
.into()
}
}
enum State {
Disconnected,
Connected(echo::Connection),
}
impl Default for State {
fn default() -> Self {
Self::Disconnected
}
}
|