Multiplayer game using Nodejs and Socket IO

I tried a multiplayer racing game using Node and Socket IO, express. So I tried a simple example to see the delay between the Node server and the clients. I have a drag and drop image in the client. when i move the image ienter code heren one client, it should move across all clients. so basically when i move the image i send the image position to the Node server in json format and then transfer all clients from there. from time to time is approximately 300 ms. Below are the results.

Client 1 sends data to the server at: 286136 (time stamp) Server received at: 286271

Client2 received data at: 286470 Client3 received data at: 286479 Client4 received data at: 286487 Client5 received data at: 286520

The delay between the transition from client1 to client5 is 384 ms. its height is too high for a racing game. here is my server code.

var app = require('express').createServer(); var io = require('socket.io'); var http = require('http'); var http_server = http.createServer(); var server = http.createServer(app); server.listen(3000); var socket = io.listen(server,{ log: false }); socket.sockets.on('connection', function (client) { client.on('message', function (data){ console.log("data arrived to server",new Date().getTime()); // Below both statements are giving same latency between the client 1 and client 5 client.broadcast.emit('message',data); //socket.sockets.emit('message',data); }); }); 

1) Is there a way to optimize server code to reduce latency?
2) is the expected latency using Node and websockets?
3) The io socket cannot transmit data asynchronously (I mean at the same time)?

Thanks Kishorevarma

+4
source share
2 answers

I created a couple of real-time games like this one. One of them was a multiplayer asteroid (everyone shot at asteroids, a cooperative). I had a great response time - BUT, I did NOT "send spam" to all clients with too much data - this can be a problem. I got a game to run at 60 frames per second on a client with a server processing physics at 30 frames per second. It only worked when all the clients were Chrome (so they had web sockets). Most browsers today support network sockets. I would try to make sure you have sockets. Secondly, I would not send too much data to all clients: one way is to process the physics on the server at a known speed (for example, 30 frames per second). In addition, you need to handle the physics on the client. Send changes to all customers at the 30 fps border (not when receiving data) to end users. For asteroids, this means: Each client AND server knows the following:

  • Where each player is a ship, and everything about this ship (reversed direction, floating direction, if boost is on or off)
  • Where is each bullet (location, direction, speed, who produced it)
  • Where is each asteroid (location, direction, speed)
  • 30 times per second, run physics, repaint the screen

Now...

  • When the client gives you a request (for example, turn my ship to right) - the client sends this data to the server (and DO NOT process it locally)
  • When the server receives a user request, it queues the request until the next "tick" is processed
  • Server tick (30 times per second): handle all events in the queue:
  • At the request of turning the ship: actually turn the ship. Broadcast ship data to everyone (location, direction in the opposite direction, speed ...)
  • When a client receives an update event from the server, it fills this new β€œcorrect” data from the server into this local space. Note. If the client cannot process everything at a speed of 30 frames per second, the ships will "jump" to their correct locations when sending data.
+3
source

You can check out my multiplayer online game written using Node and Socket.io - https://github.com/ecdeveloper/wallattack . The code is really simple.

There is also a demo version of http://wallattack.ecdeveloper.com/

0
source

All Articles