WebSocketServer
This commit is contained in:
40
packages/server/src/http/WebSocketServer.ts
Normal file
40
packages/server/src/http/WebSocketServer.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { applyWSSHandler } from "@trpc/server/adapters/ws"
|
||||||
|
import { Context, Effect, Layer, Runtime } from "effect"
|
||||||
|
import ws from "ws"
|
||||||
|
import { ExpressHTTPServer } from "../http/ExpressHTTPServer"
|
||||||
|
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
||||||
|
|
||||||
|
|
||||||
|
export class WebSocketServer extends Context.Tag("RPCWebSocketServer")<WebSocketServer, {
|
||||||
|
wss: ws.Server
|
||||||
|
handler: ReturnType<typeof applyWSSHandler>
|
||||||
|
}>() {}
|
||||||
|
|
||||||
|
export module WebSocketServer {
|
||||||
|
export const Live = Layer.effect(WebSocketServer, Effect.acquireRelease(
|
||||||
|
Effect.gen(function*() {
|
||||||
|
const runSync = yield* Effect.runtime().pipe(
|
||||||
|
Effect.map(Runtime.runSync)
|
||||||
|
)
|
||||||
|
|
||||||
|
const wss = new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
|
||||||
|
runSync(Effect.logInfo(`WebSocket RPC server started`))
|
||||||
|
)
|
||||||
|
|
||||||
|
const handler = applyWSSHandler({
|
||||||
|
wss,
|
||||||
|
router: yield* RPCRouter,
|
||||||
|
createContext: yield* TRPCContextCreator,
|
||||||
|
})
|
||||||
|
|
||||||
|
return { wss, handler }
|
||||||
|
}),
|
||||||
|
|
||||||
|
({ wss, handler }) => Effect.gen(function*() {
|
||||||
|
yield* Effect.logInfo(`WebSocket server is closing. Waiting for existing connections to end...`)
|
||||||
|
yield* Effect.async(resume => {
|
||||||
|
wss.close(() => resume(Effect.logInfo(`WebSocket server closed`)))
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user