-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,47 @@ | ||
import worker_threads from "node:worker_threads"; | ||
import { cpus } from "node:os"; | ||
import url from "node:url"; | ||
import path from "node:path"; | ||
import uws from "uWebSockets.js"; | ||
import worker_threads from 'node:worker_threads' // eslint-disable-line | ||
import { cpus } from 'node:os' | ||
import url from 'node:url' | ||
// import path from 'node:path' | ||
import uws from 'uWebSockets.js' | ||
|
||
var __filename = url.fileURLToPath(import.meta.url); | ||
var __dirname = path.dirname(__filename); | ||
const __filename = url.fileURLToPath(import.meta.url) | ||
// const __dirname = path.dirname(__filename) | ||
|
||
// eslint-disable-next-line | ||
if (worker_threads.isMainThread) { | ||
for (let i = cpus().length - 1; i >= 0; --i) { | ||
new worker_threads.Worker(__filename); | ||
// eslint-disable-next-line | ||
new worker_threads.Worker(__filename) | ||
} | ||
} else { | ||
const app = uws.App(); | ||
const app = uws.App() | ||
|
||
app | ||
.ws("/*", { | ||
.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); | ||
}, | ||
const ok = ws.send(message, isBinary) // eslint-disable-line | ||
} | ||
}) | ||
.get("/*", (res, req) => { | ||
.get('/*', (res, req) => { | ||
/* It does Http as well */ | ||
res | ||
.writeStatus("200 OK") | ||
.end("Hello there!"); | ||
.writeStatus('200 OK') | ||
.end('Hello there!') | ||
}) | ||
.listen(5001, (listenSocket) => { | ||
if (listenSocket) { | ||
// eslint-disable-next-line | ||
if (worker_threads.threadId === 0) { | ||
console.log("Listening to port 5001"); | ||
console.log('Listening to port 5001') | ||
} else { | ||
// eslint-disable-next-line | ||
console.log(`Listening to port 5001 from thread ${worker_threads.threadId}`) | ||
} | ||
} | ||
}); | ||
}) | ||
} |