repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: cleanup dead code
[poolifier.git]
/
examples
/
typescript
/
websocket-server-pool
/
ws-cluster
/
src
/
worker.ts
diff --git
a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts
b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts
index efe0facdc900b96ae4b4d49d36da3729a598147a..18ece026c891efb3106499667027a1a2aba0b73a 100644
(file)
--- a/
examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts
+++ b/
examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts
@@
-8,27
+8,28
@@
import {
type WorkerResponse
} from './types.js'
type WorkerResponse
} from './types.js'
-const factorial: (n: number) => number = (n) => {
- if (n === 0) {
- return 1
- }
- return factorial(n - 1) * n
-}
-
class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static wss: WebSocketServer
class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
private static wss: WebSocketServer
+ private static readonly factorial = (n: number): number => {
+ if (n === 0) {
+ return 1
+ }
+ return WebSocketServerWorker.factorial(n - 1) * n
+ }
+
private static readonly startWebSocketServer = (
workerData?: WorkerData
): WorkerResponse => {
private static readonly startWebSocketServer = (
workerData?: WorkerData
): WorkerResponse => {
- const { port } = workerData as WorkerData
+ const { port } = workerData!
+
WebSocketServerWorker.wss = new WebSocketServer({ port }, () => {
console.info(
`⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
)
})
WebSocketServerWorker.wss = new WebSocketServer({ port }, () => {
console.info(
`⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
)
})
- WebSocketServerWorker.wss.on('connection',
(ws)
=> {
+ WebSocketServerWorker.wss.on('connection',
ws
=> {
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
@@
-48,7
+49,9
@@
class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
ws.send(
JSON.stringify({
type: MessageType.factorial,
ws.send(
JSON.stringify({
type: MessageType.factorial,
- data: { number: factorial(data.number as number) }
+ data: {
+ number: WebSocketServerWorker.factorial(data.number!)
+ }
})
)
break
})
)
break
@@
-62,7
+65,11
@@
class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
}
public constructor () {
}
public constructor () {
- super(WebSocketServerWorker.startWebSocketServer)
+ super(WebSocketServerWorker.startWebSocketServer, {
+ killHandler: () => {
+ WebSocketServerWorker.wss.close()
+ }
+ })
}
}
}
}