Skip to content

freitas-labs/ws-multiplayer-tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ws-multiplayer-tic-tac-toe

Getting an hands-on on websockets with a multiplayer tic tac toe

Summary

Never had prior experience with websockets and wanted to get the hands dirty with a quick project. Decide to build a multiplayer tic-tac-toe.

Some of the things I've learned while building this project:

  • Websockets always start with a GET HTTP Request and the headers "Upgrade: WebSocket; Connection: Upgrade", and the server responds with 101 Switching Protocols
  • The mental model for designing APIs with websockets is completely different than the traditional "REST" APIs. In traditional REST APIs, routing is based on the request path, method, parameters, etc. In websockets, routing needs to be handled via the received body since "requests" are sent in a single connection.
  • Transmitting data in websockets is really FAST
  • To achieve broadcast replies you gotta keep track of the open connection clients
  • To close a connection, the client & server can disconnect. Additionally, a ping-pong mechanism is used to validate if the client/server is still awake.
  • A websocket server can be mixed with an HTTP server on the same port (this is possible since a websocket connection starts with a HTTP request)

image

Demo

2024-06-06.18-46-28.mp4