Learning to code Rust for a http + WebSocket server on the same port
version: 2.0 date: 2019-04-16 author: bestia.dev repository: GitHub
Hashtags: #rustlang #game #tutorial
My projects on Github are more like a tutorial than a finished product: bestia-dev tutorials.
using Warp for a simple memory game for kids - mem2.
The Http server is just a simple static file server. All the logic is in Wasm in the browser.
The browser uses the root route /. The served files are in the folder /mem2/.
The WebSocket server just broadcasts the received msg to all other connected clients.
TODO: limit communication only between 2 players. Broadcasting is a an overkill. How to get the WS client id on the client?
You can play the game here (hosted on google cloud platform):
The frontend Rust Wasm Dodrio Virtual Dom application code is here:
Build and Serve locally
git clone email@example.com:bestia-dev/mem2_server.git
Run in mem2_server/ folder
The server will print the External IP Address e.g. 192.168.0.22
Open your browser and use that address.
The game is made for exactly 2 players. Open 2 browser windows with the same address.
Preferably on 2 smartphones on the same WiFi network.
The frontend files are all in the folder mem2/.
You can replace them eventually with the new version built with wasm-pack from the project
Memory game rules
This game is for exactly 2 players.
The first player clicks on "Want to play?" and broadcasts the message over WebSocket.
Player2 then sees on the screen a "Accept the game" link, clicks it and sends the message to Player1.
The game starts with a grid of 8 randomly shuffled card pairs face down - 16 cards in all.
On the screen under the grid are clear signals which player plays and which waits.
Player1 flips over two cards with two clicks.
If the cards do not match, the other player clicks on "Take your turn" and both cards are flipped back face down. Then it is his turn and he clicks to flip over his two cards.
If the cards match, they are left face up permanently and the player receives a point. He continues to play, he opens the next two cards.
The adventure never ends
There is so much to learn. That is also the goal of this project.
How to use warp for static file server and WebSocket on the same port.
How to route the request to some function (filter).
How to use #cfg to have different codes for Linux and windows.
How to start a command and get the output and parse it with regex.
How to use env_logger to write to the screen and with nanoseconds and colors.
How to parse cmdline parameters with defaults.
cargo crev reviews and advisory
It is recommended to always use cargo-crev
to verify the trustworthiness of each of your dependencies.
Please, spread this info.
On the web use this url to read crate reviews. Example: