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: A piece of interesting test code. #3

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

Reminder: A piece of interesting test code. #3

trasherdk opened this issue Dec 6, 2021 · 0 comments

Comments

@trasherdk
Copy link
Owner

Saving a reference to websockets#1940 (comment)

const net = require('net');
const stream = require('stream');

const data = Buffer.alloc(15 * 1024 * 1024);

const server = net.createServer(function (socket) {
  function onData(chunk) {
    if (!writable.write(chunk)) {
      socket.pause();
    }
  }

  const writable = new stream.Writable({
    write(chunk, encoding, callback) {
      callback(new Error('Oops'));
    }
  });

  writable.on('error', function (err) {
    console.error(err);

    socket.end();
    socket.removeListener('data', onData);
    socket.resume();
  });

  socket.on('data', onData);
  socket.on('end', function () {
    console.log('server end');
  });
});

server.listen(function () {
  const { port } = server.address();
  const socket = net.createConnection({ port });

  socket.on('connect', function () {
    socket.write(data);
  });

  socket.on('end', function () {
    console.log('client end');
  });

  socket.resume();
});

For this problem: websockets#1940 (comment)

// this program:
//  * starts an http server on port 8545
//  * the http server hands off WebSocket upgrades to a `ws` Server
//  * then sends a WebSocket payload to the server that is over it's `maxPayload` size
//  * it "handles" the server-side `ws` error by listening to the "error" event
//  * it expects the WebSocket connection to close immediately, but on node 10-12 it doesn't

const http = require("http");
const WebSocket = require("ws");
const MAX_PAYLOAD_SIZE = 15 * 1024 * 1024; // an arbitrary size
const PORT = 8545;

const server = http.createServer();
const websocketServer = new WebSocket.Server({
  noServer: true, // we want to use our own `http` server
  maxPayload: MAX_PAYLOAD_SIZE,
});

function handleHttpUpgrade(request, socket, head) {
  websocketServer.handleUpgrade(request, socket, head, (ws, incomingMessage) => {
    // handle websocket errors, like "Max payload size exceeded":
    ws.on("error", (error) => {
      console.log("ws error", error);
      console.log("ws errored at", new Date());
    });

    ws.on("close", () => {
      // log the current time when the server-side websocket is closed
      console.log("ws closed at", new Date());
    });
  });
}

function listening() {
  const clientWebsocket = new WebSocket(`ws://127.0.0.1:${PORT}`);
  clientWebsocket.on("open", () => {
    // send too much data:
    clientWebsocket.send(Buffer.alloc(MAX_PAYLOAD_SIZE + 1));
  });

  clientWebsocket.on("close", () => {
    // log the current time when the client websocket is closed
    console.log("clientWebsocket closed at", new Date());
  });
}

server.on("upgrade", handleHttpUpgrade);
server.listen({ port: PORT }, listening);
Repository owner locked and limited conversation to collaborators Mar 22, 2022
@trasherdk trasherdk converted this issue into discussion #8 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