Files
bun.sh/bench/websocket-server/chat-server.node.mjs
Jarred Sumner 9388b3f825 Add a zig fmt action (#2277)
* Add a zig fmt action

* add failing file

* Setup prettier better

* Update prettier-fmt.yml

* Fail on error

* Update prettier-fmt.yml

* boop

* boop2

* tar.gz

* Update zig-fmt.yml

* Update zig-fmt.yml

* Update zig-fmt.yml

* Update zig-fmt.yml

* Update zig-fmt.yml

* boop

* Update prettier-fmt.yml

* tag

* newlines

* multiline

* fixup

* Update zig-fmt.yml

* update it

* fixup

* both

* w

* Update prettier-fmt.yml

* prettier all the things

* Update package.json

* zig fmt

*  

* bump

* .

* quotes

* fix prettier ignore

* once more

* Update prettier-fmt.yml

* Update fallback.ts

* consistentcy

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
2023-03-02 19:02:10 -08:00

49 lines
1.3 KiB
JavaScript

// See ./README.md for instructions on how to run this benchmark.
const port = process.env.PORT || 4001;
const CLIENTS_TO_WAIT_FOR = parseInt(process.env.CLIENTS_COUNT || "", 10) || 16;
import { createRequire } from "module";
const require = createRequire(import.meta.url);
var WebSocketServer = require("ws").Server,
config = {
host: "0.0.0.0",
port,
},
wss = new WebSocketServer(config, function () {
console.log(`Waiting for ${CLIENTS_TO_WAIT_FOR} clients to connect..`);
});
var clients = [];
wss.on("connection", function (ws, { url }) {
const name = new URL(new URL(url, "http://localhost:3000")).searchParams.get("name");
console.log(`${name} connected (${CLIENTS_TO_WAIT_FOR - clients.length} remain)`);
clients.push(ws);
ws.on("message", function (message) {
const out = `${name}: ${message}`;
for (let client of clients) {
client.send(out);
}
});
// when a connection is closed
ws.on("close", function (ws) {
clients.splice(clients.indexOf(ws), 1);
});
if (clients.length === CLIENTS_TO_WAIT_FOR) {
sendReadyMessage();
}
});
function sendReadyMessage() {
console.log("All clients connected");
setTimeout(() => {
console.log("Starting benchmark");
for (let client of clients) {
client.send(`ready`);
}
}, 100);
}