RPCWebSocketServer

This commit is contained in:
Julien Valverdé
2024-07-11 06:28:05 +02:00
parent b8210ddd66
commit 4f688719d7
3 changed files with 30 additions and 25 deletions

View File

@@ -1,19 +1,27 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer, Runtime } from "effect"
import { WebSocketServer } from "ws"
import { websocketPort } from "../config"
import { Context, Effect, Layer, Runtime } from "effect"
import ws from "ws"
import { ExpressHTTPServer } from "../express/ExpressHTTPServer"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
export class RPCWebSocketServer extends Context.Tag("RPCWebSocketServer")<RPCWebSocketServer, {
wss: ws.Server
handler: ReturnType<typeof applyWSSHandler>
}>() {}
export module RPCWebSocketServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
export const Live = Layer.effect(RPCWebSocketServer, Effect.acquireRelease(
Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
const wss = new WebSocketServer({ port: yield* websocketPort })
// Extract this to its own service?
const wss = new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
runSync(Effect.logInfo(`WebSocket RPC server started`))
)
const handler = applyWSSHandler({
wss,
@@ -24,7 +32,7 @@ export module RPCWebSocketServer {
return { wss, handler }
}),
({ wss, handler }) => Effect.sync(() => {
({ wss, handler }) => Effect.gen(function*() {
handler.broadcastReconnectNotification()
wss.close()
}),