This commit is contained in:
Julien Valverdé
2024-07-12 01:15:10 +02:00
parent a6a75ad875
commit e489815fe8
2 changed files with 8 additions and 13 deletions

View File

@@ -20,9 +20,9 @@ export module ExpressHTTPServer {
}), }),
server => Effect.gen(function*() { server => Effect.gen(function*() {
yield* Effect.logInfo(`HTTP server is stopping. Waiting for existing connections to end...`) yield* Effect.logInfo("HTTP server is stopping. Waiting for existing connections to end...")
yield* Effect.async(resume => { yield* Effect.async(resume => {
server.close(() => resume(Effect.logInfo(`HTTP server closed`))) server.close(() => resume(Effect.logInfo("HTTP server closed")))
}) })
}), }),
)) ))

View File

@@ -1,26 +1,21 @@
import { Context, Effect, Layer, Runtime } from "effect" import { Context, Effect, Layer } from "effect"
import ws from "ws" import ws from "ws"
import { ExpressHTTPServer } from "../http/ExpressHTTPServer" import { ExpressHTTPServer } from "../http/ExpressHTTPServer"
export class WebSocketServer extends Context.Tag("RPCWebSocketServer")<WebSocketServer, ws.Server>() {} export class WebSocketServer extends Context.Tag("WebSocketServer")<WebSocketServer, ws.Server>() {}
export module WebSocketServer { export module WebSocketServer {
export const Live = Layer.effect(WebSocketServer, Effect.acquireRelease( export const Live = Layer.effect(WebSocketServer, Effect.acquireRelease(
Effect.gen(function*() { Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe( yield* Effect.logInfo("WebSocket server started")
Effect.map(Runtime.runSync) return new ws.WebSocketServer({ server: yield* ExpressHTTPServer })
)
return new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
runSync(Effect.logInfo(`WebSocket server started`))
)
}), }),
wss => Effect.gen(function*() { wss => Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server is stopping. Waiting for existing connections to end...`) yield* Effect.logInfo("WebSocket server is stopping. Waiting for existing connections to end...")
yield* Effect.async(resume => { yield* Effect.async(resume => {
wss.close(() => resume(Effect.logInfo(`WebSocket server closed`))) wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
}) })
}), }),
)) ))