Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reminder: Test server and clients sample code #5

Closed
trasherdk opened this issue Dec 20, 2021 · 0 comments
Closed

Reminder: Test server and clients sample code #5

trasherdk opened this issue Dec 20, 2021 · 0 comments

Comments

@trasherdk
Copy link
Owner

trasherdk commented Dec 20, 2021

These snippets was made for testing deflate memory problems.

server.js and clients.js was copied from: websockets#1369 (comment)

server.js

'use strict';

const WebSocket = require('ws');

const message = Buffer.alloc(256);

const wss = new WebSocket.Server({
  port: 8080,
  perMessageDeflate: {
    zlibDeflateOptions: {
      memLevel: 3
    },
    serverMaxWindowBits: 10,
    threshold: 16
  }
});

function onClose() {
  if (wss.clients.size === 0) {
    console.log('connections: 0');
  }
}

function broadcast() {
  console.log('Broadcasting message');

  for (const ws of wss.clients) {
    ws.send(message);
    ws.close();
  }
}

wss.on('connection', (ws) => {
  if (wss.clients.size === 1000) {
    console.log('connections: 1000');
    broadcast();
  }

  ws.on('close', onClose);
});

wss.on('listening', function() {
  console.log('Listening on *:8080');
});

setInterval(function() {
  console.log(process.memoryUsage());
}, 10000);

clients.js

'use strict';

const WebSocket = require('ws');

const total = 1000;
let connected = 0;

function connect() {
  const ws = new WebSocket('ws://localhost:8080/');

  ws.on('open', function() {
    if (++connected < total) connect();

    ws.on('close', function() {
      if (--connected === 0) setTimeout(connect, 5000);
    });
  });
}

connect();

Another, single file server / client test, copied from websockets#1369 (comment)

server-client.js

const WebSocket = require('ws');

const wss = new WebSocket.Server({
  port: 8080,
  perMessageDeflate: {
    zlibDeflateOptions: {
      memLevel: 3,
    },
    serverMaxWindowBits: 10,
    threshold: 16,
  },
});

wss.on('connection', (ws) => {
  ws.on('message', (message) => {
    ws.send(message);
  });
  ws.on('close', () => {
    console.log(`close, ${wss.clients.size} listeners left`);
  });
});

const message = Buffer.alloc(256);

function sendMessage(ws, j) {
  if (j < 100) {
    setTimeout(() => {
      ws.send(message, sendMessage.bind(null, ws, j + 1));
    }, 16);
  } else {
    ws.close();
  }
}

for (let i = 0; i < 1000; ++i) {
  const ws = new WebSocket('ws://localhost:8080/');
  ws.on('open', () => {
    sendMessage(ws, 0);
  });
}
Repository owner locked and limited conversation to collaborators Mar 22, 2022
@trasherdk trasherdk converted this issue into discussion #10 Mar 22, 2022
@trasherdk trasherdk reopened this May 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant