Server Client communication based on Socket.IO protocol
Client Side * : | Server side: |
---|---|
HTML, CSS, JS (including SocketIO framework) | Python frameworks: Flask, Flask SocketIO |
*front-end elements of Tic Tac Toe board inspired from Web Dev Simplified Tic Tac Toe project - one side 2 player functionality
In Online Multiplayer Tic-Tac-Toe game, processing the moves, handling the connection of the players, players chat and gaming rooms management is provided by the server (in this case with the help of Flask, Flask SocketIO frameworks). As per tic tac toe rules, there are only two players in one game. This mean that the server has to check the existence of gaming room and if there are already two players before connecting a new one.
Step 1 - Connection
Step 2 - Player is informed
Step 3 - startGame
app.py
The file where all the fun starts...
Here is the configuration of Flask and Flask Socket IO frameworks.
Once configured, the Flask routes are decorated with @app.route()
and Flask SocketIO handlers are decorated with @socketio.event
.
At the bottom of the file, there's socketio.run(app, debug=True)
that will start the server.
"The socketio.run()
function encapsulates the startup of the web server and replaces the app.run()
standard Flask development server start up." Flask SocketIO
oophelpers.py
There’s the modeling of Player()
and GameRoom()
objects. Within itself, classes define behavior and needed attributes.
templates/
Basically, the templates/index.html
is just an HTML page that describe the content of the page - board game and logs area. On top of the page there's "Greetings view" as a modal view that will change the state: display: show
-> display: none
when the server event is emit the room availability.
static/styles/
That's where are the CSS files used to describe the aesthetics of the page.
static/scripts/
In the main.js
file are the scripts that make webapp interaction possible.
The file starts with the "VARIABLE DECLARATIONS" at the top, afterwards on joinButton
's addEventListener
anonymous function is verified the roomAvailability()
by the mean of async
& await
functions. Once the room availability is confirmed, the userConnectedHandlers()
are called. The chat and game function are provided throughout the EventListener attached to sendButton, startGameButton, restartButton buttons.
requirements.txt
That file simply prescribes the dependencies packages for this web app.
One way to run this application:
python -m venv [directory]
pip install -r requirements.txt
python app.py
or flask run
and visit http://localhost:5000
in two separate browser tabs.python app.py
or
flask run
Build an Image from the Dockerfile
docker build -t <img_name> .
docker build -t flask-tictactoe .
Run a container in background with and publish a container’s port(s) to the host
docker run -d -p <host_port>:<container_port> <image_name>
docker run -d -p 80:5000 flask-tictactoe
Acces the container in browser using: http://localhost
or http://127.0.0.1