Skip to content

Commit

Permalink
use uws
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Aug 21, 2024
1 parent 7ac6679 commit 3f6a83e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"node-fetch": "^3.3.2",
"request": "^2.88.2",
"superagent": "^10.0.0",
"wait-on": "^7.2.0"
"wait-on": "^7.2.0",
"uWebSockets.js": "uNetworking/uWebSockets.js#v20.47.0"
}
}
46 changes: 39 additions & 7 deletions benchmarks/websocket/server/echo.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import cluster from 'node:cluster'
import { cpus } from 'node:os'
import worker_threads from "node:worker_threads";

Check failure on line 1 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Identifier 'worker_threads' is not in camel case

Check failure on line 1 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 1 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
import { cpus } from "node:os";

Check failure on line 2 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 2 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
import url from "node:url";

Check failure on line 3 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 3 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
import path from "node:path";

Check failure on line 4 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 4 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
import uws from "uWebSockets.js";

Check failure on line 5 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

if (cluster.isPrimary) {
let cpu = cpus().length
while (cpu-- > 0) {
cluster.fork()
var __filename = url.fileURLToPath(import.meta.url);

Check warning on line 7 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead
var __dirname = path.dirname(__filename);

Check warning on line 8 in benchmarks/websocket/server/echo.mjs

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead

if (worker_threads.isMainThread) {
for (let i = cpus().length - 1; i >= 0; --i) {
new worker_threads.Worker(__filename);
}
} else {
// use uws?
const app = uws.App();

app
.ws("/*", {
compression: uws.DISABLED,
maxPayloadLength: 512 * 1024 * 1024,
maxBackpressure: 128 * 1024,
idleTimeout: 60,
message: (ws, message, isBinary) => {
/* Here we echo the message back, using compression if available */
const ok = ws.send(message, isBinary);
},
})
.get("/*", (res, req) => {
/* It does Http as well */
res
.writeStatus("200 OK")
.end("Hello there!");
})
.listen(5001, (listenSocket) => {
if (listenSocket) {
if (worker_threads.threadId === 0) {
console.log("Listening to port 5001");
} else {
console.log(`Listening to port 5001 from thread ${worker_threads.threadId}`)
}
}
});
}

0 comments on commit 3f6a83e

Please sign in to comment.