cs6413110
cs6413110

Reputation: 67

Websocket server has massive delay in multiplayer game

I am coding a multiplayer game and use a Websocket Server to send messages between clients for the game to work. During random times in the game, there is a massive, absolutely massive delay in the message time between the messages ONLY sent from the server to the client. The delay from the client to the server does not increase during this. This only happens periodically, most of the time the game works fine.

I built a ping meter to measure the time between the client sends a message and the server receives it and vica versa. In the server to client, during random periods, the ping skyrockets. I'm talking like 20+ seconds. It doesn't immediately jump. It increases steadily.

I can't put all the code for the game here(over 10,000 lines), but here is how the game works:

Server send a hostupdate to all sockets connected to the room.

Client sends a joinerupdate to server:

I am using the WS npm module to run my WebSocket Server and using the default browser built in websocket to connect to my server.

Upvotes: 2

Views: 1650

Answers (1)

cs6413110
cs6413110

Reputation: 67

SOLUTION:

On the WS npm package site I found an example that gives better performance for sending messages. I no longer have a problem :D

import WebSocket, { WebSocketServer } from 'ws';

const wss = new WebSocketServer({
  port: 8080,
  perMessageDeflate: {
    zlibDeflateOptions: {
      // See zlib defaults.
      chunkSize: 1024,
      memLevel: 7,
      level: 3
    },
    zlibInflateOptions: {
      chunkSize: 10 * 1024
    },
    // Other options settable:
    clientNoContextTakeover: true, // Defaults to negotiated value.
    serverNoContextTakeover: true, // Defaults to negotiated value.
    serverMaxWindowBits: 10, // Defaults to negotiated value.
    // Below options specified as default values.
    concurrencyLimit: 10, // Limits zlib concurrency for perf.
    threshold: 1024 // Size (in bytes) below which messages
    // should not be compressed if context takeover is disabled.
  }
});

Upvotes: 2

Related Questions