In this exercise you will learn how to
-
use websockets to communicate messages between frontend and web server
-
use the
env_logger
crate -
implement custom message handler(s)
The web server runs at http://localhost:8080. Once started, open the link in a browser. The rudimentary UI allows you to connect to the web server, meaning you will join the chat server. Once connected you can write messages in the text input field. Received messages will be displayed in the text box.
Tasks
-
Broadcast the client message to all other connected clients
-
to see the effect connect at least twice in separate tabs
-
-
Implement a custom message to disconnect from the chat server
-
you should not receive new messages from other clients thereafter
-
notify other clients of disconnect
-
-
Check that you do not receive your own chat message back
-
Add heartbeat logic to remove a client from the chat server after 10s of inactivity (e.g. tab closed)
-
keep a time in
WsChatSession
to compare timeout with -
the
actix::AsyncContext::run_interval
method is used to run a background job to check if client has timed out -
see
std::time::Instant
andstd::time::Duration
to determine the time difference -
use
WebsocketContext::ping
to ping client
-
Getting Started
Clone the repository at ferrous-systems/teaching-material. You find the assignment in folder assignments/actix/chat-websockets
.
Run the web server with cargo run
to start the chat server.
The given dependencies of the Cargo.toml are not the most recent versions due to some incompatibilities. Updating these may not compile the program anymore.
|
Help
Further documentation
-
actix-web-actors API documentation, check
Message
enum -
actix::Recipient#do_send API documentation to send a message to an Actor
-
actix::ASyncContext documentation
Bonus Tasks
-
Add support to enter & display the client name
-
For this you need to adapt the
index.html
page, remove thevisibility:hidden;
style from thename
input field -
Think about how to distinguish setting the name from sending a text, e.g. using a prefix
-
Expand the
WsChatSession
struct to store the name, adapt code accordingly -
If the name is available output it instead of displaying the text
"Someone"
in code
-
-
What issues might the chat server have?
-
What is not working as you would expect?
-