/
sse_chat.rs
195 lines (170 loc) · 5.98 KB
/
sse_chat.rs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use futures::{Stream, StreamExt};
use std::collections::HashMap;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
};
use tokio::sync::{mpsc, oneshot};
use warp::{sse::ServerSentEvent, Buf, Filter};
/// Our global unique user id counter.
static NEXT_USER_ID: AtomicUsize = AtomicUsize::new(1);
/// Message variants.
enum Message {
UserId(usize),
Reply(String),
}
#[derive(Debug)]
struct NotUtf8;
impl warp::reject::Reject for NotUtf8 {}
/// Our state of currently connected users.
///
/// - Key is their id
/// - Value is a sender of `Message`
type Users = Arc<Mutex<HashMap<usize, mpsc::UnboundedSender<Message>>>>;
#[tokio::main]
async fn main() {
pretty_env_logger::init();
// Keep track of all connected users, key is usize, value
// is an event stream sender.
let users = Arc::new(Mutex::new(HashMap::new()));
// Turn our "state" into a new Filter...
let users = warp::any().map(move || users.clone());
// POST /chat -> send message
let chat_send = warp::path("chat")
.and(warp::post())
.and(warp::path::param::<usize>())
.and(warp::body::content_length_limit(500))
.and(warp::body::concat().and_then(|body: warp::body::FullBody| async move {
std::str::from_utf8(body.bytes())
.map(String::from)
.map_err(|_e| warp::reject::custom(NotUtf8))
}))
.and(users.clone())
.map(|my_id, msg, users| {
user_message(my_id, msg, &users);
warp::reply()
});
// GET /chat -> messages stream
let chat_recv =
warp::path("chat")
.and(warp::sse())
.and(users)
.map(|sse: warp::sse::Sse, users| {
// reply using server-sent events
let stream = user_connected(users);
sse.reply(warp::sse::keep_alive().stream(stream))
});
// GET / -> index html
let index = warp::path::end().map(|| {
warp::http::Response::builder()
.header("content-type", "text/html; charset=utf-8")
.body(INDEX_HTML)
});
let routes = index.or(chat_recv).or(chat_send);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
fn user_connected(
users: Users,
) -> impl Stream<Item = Result<impl ServerSentEvent + Send + 'static, warp::Error >> + Send + 'static
{
// Use a counter to assign a new unique ID for this user.
let my_id = NEXT_USER_ID.fetch_add(1, Ordering::Relaxed);
eprintln!("new chat user: {}", my_id);
// Use an unbounded channel to handle buffering and flushing of messages
// to the event source...
let (mut tx, rx) = mpsc::unbounded_channel();
match tx.try_send(Message::UserId(my_id)) {
Ok(()) => (),
Err(_disconnected) => {
// The tx is disconnected, our `user_disconnected` code
// should be happening in another task, nothing more to
// do here.
}
}
// Make an extra clone of users list to give to our disconnection handler...
let users2 = users.clone();
// Save the sender in our list of connected users.
users.lock().unwrap().insert(my_id, tx);
// Create channel to track disconnecting the receiver side of events.
// This is little bit tricky.
let (mut dtx, mut drx) = oneshot::channel::<()>();
// When `drx` will dropped then `dtx` will be canceled.
// We can track it to make sure when the user leaves chat.
warp::spawn(async move {
dtx.closed().await;
drx.close();
user_disconnected(my_id, &users2);
});
// Convert messages into Server-Sent Events and return resulting stream.
rx.map(|msg| match msg {
Message::UserId(my_id) => Ok((warp::sse::event("user"), warp::sse::data(my_id)).into_a()),
Message::Reply(reply) => Ok(warp::sse::data(reply).into_b()),
})
}
fn user_message(my_id: usize, msg: String, users: &Users) {
let new_msg = format!("<User#{}>: {}", my_id, msg);
// New message from this user, send it to everyone else (except same uid)...
//
// We use `retain` instead of a for loop so that we can reap any user that
// appears to have disconnected.
for (&uid, tx) in users.lock().unwrap().iter_mut() {
if my_id != uid {
match tx.try_send(Message::Reply(new_msg.clone())) {
Ok(()) => (),
Err(_disconnected) => {
// The tx is disconnected, our `user_disconnected` code
// should be happening in another task, nothing more to
// do here.
}
}
}
}
}
fn user_disconnected(my_id: usize, users: &Users) {
eprintln!("good bye user: {}", my_id);
// Stream closed up, so remove from the user list
users.lock().unwrap().remove(&my_id);
}
static INDEX_HTML: &str = r#"
<!DOCTYPE html>
<html>
<head>
<title>Warp Chat</title>
</head>
<body>
<h1>warp chat</h1>
<div id="chat">
<p><em>Connecting...</em></p>
</div>
<input type="text" id="text" />
<button type="button" id="send">Send</button>
<script type="text/javascript">
var uri = 'http://' + location.host + '/chat';
var sse = new EventSource(uri);
function message(data) {
var line = document.createElement('p');
line.innerText = data;
chat.appendChild(line);
}
sse.onopen = function() {
chat.innerHTML = "<p><em>Connected!</em></p>";
}
var user_id;
sse.addEventListener("user", function(msg) {
user_id = msg.data;
});
sse.onmessage = function(msg) {
message(msg.data);
};
send.onclick = function() {
var msg = text.value;
var xhr = new XMLHttpRequest();
xhr.open("POST", uri + '/' + user_id, true);
xhr.send(msg);
text.value = '';
message('<You>: ' + msg);
};
</script>
</body>
</html>
"#;