RPCWebSocketServer
This commit is contained in:
@@ -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()
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user