Socket.IO Client Setup
Installing the Client Library
The Socket.IO client is loaded via CDN in the HTML file:index.html
Establishing Connection
The client connects to the server using WebSocket transport for optimal performance:index.html:192-193
For local development, you can override the connection URL:
Alternative WebSocket Connection
Themain.js file demonstrates a direct WebSocket connection to a production server:
main.js:4
DOM Element References
The client initializes references to key DOM elements for managing the UI:index.html:194-205
UI Structure
The client interface is divided into two main sections:Lobby Section
The lobby (
#lobby) is shown when players first connect. It provides options to:- Create a new game room
- Search for available rooms
- Join a room by code
index.html:114-128
Sending Events to Server
The client sends events usingsocket.emit() to communicate player actions:
Creating a Room
index.html:349-356
Joining a Room
Sending Game Actions
Receiving Server Events
The client listens for various events from the server:Info and Error Messages
index.html:221-229
index.html:243
Room Events
salaCreada Event
salaCreada Event
Triggered when a room is successfully created:
index.html:239-241
salaLista Event
salaLista Event
Triggered when the player successfully joins a room:
index.html:231-237
Game Events
start Event
start Event
Triggered when the game begins:
index.html:245-251
turn Event
turn Event
Manages turn transitions between players:
index.html:253-268
result Event
result Event
Displays guess results and detects game completion:
index.html:270-303
Audio System Integration
The client includes a complete audio feedback system:Audio Element Setup
index.html:175-179
Audio References and Configuration
index.html:207-213
Audio Playback Events
| Event | Audio | Description |
|---|---|---|
| Secret submitted | sndEnviarSecreto | Plays when player submits secret number |
| Game starts | sndJuego | Background music (loops continuously) |
| Turn begins | sndTurno | Plays when it’s the player’s turn |
| Player wins | sndGanador | Victory sound |
| Player loses | sndPerdedor | Defeat sound |
The background game music (
sndJuego) is paused when the game ends to stop playback:Result Display Overlay
The overlay system displays end-game results:index.html:181
index.html:339-343