Skip to content

A multiplayer chess game made with React and Gorilla WebSocket.

Notifications You must be signed in to change notification settings

romulodm/go-chess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 

Repository files navigation

🔎 Overview

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


🌱 Technologies

Front-end:

  • React
  • Chess.js
  • Chessboardjsx
  • Material UI
  • Tailwind CSS

Back-end:

  • Go
  • net/http
  • Gorilla WebSocket

📰 Description

We have implemented rooms that can have a maximum of two users connected, that is, they are private rooms for a chess game. We use the Gorilla WebSocket framework to create Rooms that have an identification ID, used to enter a room.

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
}

In other words, we have two endpoints that upgrade the connection, the first is to create a room and the other is to enter an existing room. In these two endpoints we receive a front-end ID.

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,
	}
}
Component that makes the requests

Figure 2: Lobby component

Figure 3: Game page


🔌 WebSocket

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.


Figure 4: Differences between HTTP and WebSocket



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.


👥 Contributors