Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use websockets for real-time gaming

I want to make a 2 player pong game that uses websockets and node.js server. socket.io is used on both client and server. So far, my only experience is creating a chat application.

This is my first attempt at a multiplayer game so I'm not so familiar with network gaming. Should the server keep track of:

  1. Every position the ball is at and how often or when?
  2. player movement, player move left or right, what if I press and hold for awhile, how do I handle this? Should I send like a pressHoldStartPosition and pressHoldStopPosition? I guess this is easy if I only allow pressing but not holding down.

My thoughts:

  1. When the ball hits a player, the client calculates velocity, start and end position and the other client should perform the correct animation from that.
  2. No idea.
like image 251
Shawn Mclean Avatar asked Jan 20 '12 05:01

Shawn Mclean


People also ask

Is WebSocket good for gaming?

For web games, generally you'd use something like WebSockets. That's still not ideal, but way better than HTTP.

Is WebSockets real time?

WebSockets are used for real-time communication between the browser and a server. These communications are bi-directional. Therefore it fits low latency applications such as chat, real-time multiplayer gaming, in-app notifications.

Does League of Legends use WebSockets?

The League Client runs a WebSocket for an event bus which anything using the client may connect to. Developers may also connect to this socket over wss. LeagueConnect provides a function to retrieve a WebSocket connection.

Do people still use WebSockets?

For now, I think there are plenty of applications already using Websocket. And there are many frameworks developed for Websockets in almost all the programming languages. They are very matured as well. Only caveat developers may think of Websocket is that, the fact that it's not HTTP.


1 Answers

You are going to find books, upon books, about gaming networking.

In a brief description, here is what I find an interesting implementation: The server and the client calculate the physics of the game. The server will be a master, and have a correct state of all the entities, while the client will attempt to "predict" the state of the entities and keep a smooth animation. The client will get updated the correct state of entities from server periodically. In some games you might see entities suddenly teleport, and that is because client predicted position does not match the server actual position.

An entity state, such as the ball, or the players, usually contains information such as the position, velocity, button state (key down/key up).

Besides that, it is letting your imagination go wild and testing for the best solution and seeing what works. There are a lot to take into consideration, such as player latency and bandwidth.

like image 104
Nican Avatar answered Oct 07 '22 18:10

Nican