Skip to content

Aravin008/tic-tac-toe-websocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic-Tac-Toe WebSocket Setup Guide

This project provides two different WebSocket-based setups for a Tic-Tac-Toe game:

  1. Basic WebSocket (ws package) – A simple WebSocket server using the ws npm package.
  2. Socket.io with Express Backend – A WebSocket server using socket.io integrated with an Express server.

1. Basic WebSocket (ws package)

This setup uses the ws npm package to create a WebSocket server on port 8080. The client code is a simple index.html file that connects to the WebSocket server.

Features

  • WebSocket server runs on port 8080.
  • The client (index.html) can be opened via a file explorer or a live server.
  • Two instances of the same page must be opened to play.
  • The game starts when two players are connected.
  • Moves and messages are communicated between the two players in real-time.
  • No user-level identity check is implemented, so the same user can connect multiple times.

How to Start

  1. Clone the project:
    git clone <repository-url>
    cd tic-tac-ws
  2. Install dependencies:
    npm install
  3. Start the WebSocket server:
    npm run dev   # Development mode
    npm run start # Production mode
  4. Open index.html using a live server or directly in the browser.

2. WebSocket with Socket.io and Express Backend

This setup uses an Express server with socket.io to handle WebSocket connections on the same port as the web server.

Features

  • WebSocket server runs alongside an Express backend.
  • The game page is accessible via http://localhost:3000/.
  • Client and server communicate over predefined socket.io events.
  • Game win/draw status handling is implemented.
  • User identity is maintained using browser cookies, preventing the same user from connecting multiple times as different players.

How to Start

  1. Clone the project if not already done:
    git clone <repository-url>
  2. Navigate to the Express-based setup:
    cd tic-tac-ws/express-socketio
  3. Install dependencies:
    npm install
  4. Start the WebSocket + Express server:
    npm run dev:socketio   # Development mode
    npm run start:socketio # Production mode
  5. Open the game page in a browser:
    http://localhost:3000/
    

Notes

  • Ensure Node.js is installed before running the project. Node@18 and above.
  • For the basic WebSocket version, use a live server for better performance.
  • Both setups require two players to start a game.
  • The Express + Socket.io version maintains a browser-level identity check using cookies, preventing duplicate connections from the same user.

Happy Coding! 🎮

About

Simple Tic-Tac-Toe over network using websocket. Using Javascript and node.js.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors