This repo show how to implement the multiplayer pong game by Websocket. It use the package of socket.io to achieve the double channel communication and the server is built by node.js and express.
The screenshot of multiplay pong game |
On the server side, it can monitor how many tabs are connected to this URL, and display the room numbers that are paired with each other.
The record of server side |
Here are some things that have to notice when developing:
- Game ready and start: To confirm two players are paired with each other and the secondary wil be referee to start the game.
- The data of paddle moving: The client side always emit his paddle position and the server side will get and broadcast the other one's paddle position.
- The data of ball moving: On the client side, it will emit the ball position and get it from the server side immediately.
The design channel of socket |
-
Make sure you already environment of node.js and install express
npm install npm install express
-
Install the package of socket.io
npm install socket.io
-
Run the server and open http://localhost:3000 in your browser
npm run start
Wen Su