This project was created for a university subject called "Programming Languages", with the objective of creating an application in a programming language that we are not very used to using, the language chosen was Go and the idea was to use WebSockets to perform real-time communication between two users in a chess match.
Figure 1: Home page
Front-end:
React
Chess.js
Chessboardjsx
Material UI
Tailwind CSS
Back-end:
Go
net/http
Gorilla WebSocket
Room
type Room struct {
ID string `json:"id"`
clients map[*Client]bool
register chan *Client
unregister chan *Client
broadcast chan *Message
}
Client
type Client struct {
Name string
conn *websocket.Conn
send chan []byte
room *Room
}
Server config
func serverConfig() http.Server {
r := mux.NewRouter()
r.HandleFunc("/create-room", func(w http.ResponseWriter, r *http.Request) {
server.CreateRoom(w, r)
})
r.HandleFunc("/join-room", func(w http.ResponseWriter, r *http.Request) {
server.JoinRoom(w, r)
})
return http.Server{
Addr: "127.0.0.1:8000",
Handler: r,
ReadTimeout: 15 * time.Second,
ReadHeaderTimeout: 15 * time.Second,
}
}
Figure 3: Game page
HTTP is a stateless communication protocol based on request and response transactions. In this model, the client sends a request to the server, which responds with the requested data. Each transaction is independent, and the connection is terminated after each interaction.
In contrast, WebSocket establishes a persistent connection between the client and server, enabling continuous bidirectional communication. This full-duplex protocol allows both parties to send and receive data simultaneously, facilitating efficient real-time message transmission without the need to initiate new transactions for each interaction, as is the case with HTTP.