'Socket.io has major delay in game
I am making a multiplayer game. I use socket.io for communication between the client and server. I have a big lag problem. Starting at around 2 seconds after the client joins the game, the server's ping to the client skyrockets. With delays that increasingly go up.
I am using a Ubuntu Oracle Compute Instance (although I have turned off the firewall and iptables)
I am using cloudflare, but I don't have any rules set, just the defaults
- I can use HTTP with my server perfectly fine however
- The sockets aren't disconnecting, and the ping between the client and the server is fine, its just the server to client.
Here is my code:
Server(WebSocket Server):
// I use the perMessageDeflate because the lag was starting within 2 seconds of joining, but now has been delayed by a few minutes.
const io = new Server(server, {
path: '/server',
perMessageDeflate: {
threshold: 4096, // defaults to 1024
zlibDeflateOptions: {
chunkSize: 8 * 1024, // defaults to 16 * 1024
},
zlibInflateOptions: {
windowBits: 15, // defaults to 15
memLevel: 9, // defaults to 8
},
clientNoContextTakeover: true, // defaults to negotiated value.
serverNoContextTakeover: true, // defaults to negotiated value.
serverMaxWindowBits: 10, // defaults to negotiated value.
concurrencyLimit: Infinity // defaults to 10 // I have tried this with a normal number value, but it works better the higher it is so I keep it at Infinity :|
}
});
Server(Where it sends to client):
joinerupdate(data) { // called when the client sends a message to the server
... // This part updates the player
// This part below sends updates to the player
var m = 0;
while (m<this.sockets.length) {
if (this.sockets[m].username == this.pt[l].username) {
var gameMessage;
if (!this.gameMessage) {
var kills = this.pt[l].kills;
gameMessage = 'Kills: ' + kills;
} else {
gameMessage = this.gameMessage;
}
this.sockets[m].emit('message', JSON.stringify({
sendTime: new Date().toISOString(),
event: 'hostupdate',
tanks: this.pt,
blocks: this.b,
scaffolding: this.scaffolding,
bullets: this.s,
gameMessage: gameMessage,
}, replacer));
}
m++;
}
}
}
l++;
}
}
NOTE: Right now I am running the messages from the client and server 45 times per second CLIENT(where client sends to the server):
send() {
user.joiner.frameOutput++;
user.joiner.socket.emit('message', JSON.stringify({
operation: 'multiplayer',
event: 'joinerupdate',
task: 'pixel-tanks',
gamemode: user.joiner.gamemode,
sendTime: new Date().toISOString(),
data: {
rank: user.joiner.tank.rank,
username: user.username,
leftright: user.joiner.tank.leftright,
x: user.joiner.tank.x,
y: user.joiner.tank.y,
rotation: user.joiner.tank.rotation,
health: user.joiner.tank.health,
fire: user.joiner.tank.fire,
xd: user.joiner.tank.xd,
yd: user.joiner.tank.yd,
rotations: user.joiner.tank.rotations,
usingToolkit: user.joiner.tank.usingToolkit,
placeScaffolding: user.joiner.tank.placeScaffolding,
scaffoldingType: user.joiner.tank.scaffoldingType,
base: user.joiner.tank.base,
shielded: user.joiner.tank.shielded,
flashbangFired: user.joiner.tank.flashbangFired,
invis: user.joiner.tank.invis,
canInvis: user.joiner.tank.canInvis,
fireType: user.joiner.tank.fireType,
canChangeInvisStatus: user.joiner.tank.canChangeInvisStatus,
immune: user.joiner.tank.immune,
type: user.joiner.tank.type,
blockShield: user.joiner.tank.blockShield,
cosmetic: userData.cosmetic,
}
}));
user.joiner.tank.scaffoldingType = null;
user.joiner.tank.blockShield = false;
user.joiner.tank.type = null;
user.joiner.tank.fire = false;
user.joiner.tank.usingToolkit = false;
user.joiner.tank.placeScaffolding = false;
user.joiner.tank.shielded = false;
user.joiner.tank.flashbangFired = false;
}
}
// This function is called with a setInterval 45 times a second
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
