RPCWebSocketServer

This commit is contained in:
Julien Valverdé
2024-07-09 02:15:18 +02:00
parent 3ac12bfe3e
commit c25c525d6f
8 changed files with 55 additions and 24 deletions

View File

@@ -1,8 +1,8 @@
import { Effect, Layer } from "effect"
import { expressHandler } from "trpc-playground/handlers/express"
import { rpcHTTPPlaygroundRoot, rpcHTTPRoot } from "../config"
import { ExpressApp } from "../express/ExpressApp"
import { RPCRouter } from "./RPCRouter"
import { rpcPlaygroundRoot, rpcRoot } from "./config"
export module RPCPlaygroundRoute {
@@ -10,10 +10,10 @@ export module RPCPlaygroundRoute {
export const Dev = Layer.effectDiscard(Effect.gen(function*() {
const app = yield* ExpressApp
const playgroundEndpoint = yield* rpcPlaygroundRoot
const playgroundEndpoint = yield* rpcHTTPPlaygroundRoot
const handler = expressHandler({
trpcApiEndpoint: yield* rpcRoot,
trpcApiEndpoint: yield* rpcHTTPRoot,
playgroundEndpoint,
router: yield* RPCRouter,
})

View File

@@ -1,16 +1,16 @@
import { createExpressMiddleware } from "@trpc/server/adapters/express"
import { Effect, Layer } from "effect"
import { rpcHTTPRoot } from "../config"
import { ExpressApp } from "../express/ExpressApp"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
import { rpcRoot } from "./config"
export module RPCServerRoute {
export const Live = Layer.effectDiscard(Effect.gen(function*() {
const app = yield* ExpressApp
app.use(yield* rpcRoot,
app.use(yield* rpcHTTPRoot,
createExpressMiddleware({
router: yield* RPCRouter,
createContext: yield* TRPCContextCreator,

View File

@@ -0,0 +1,28 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer } from "effect"
import ws from "ws"
import { websocketPort } from "../config"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
export module RPCWebSocketServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
Effect.gen(function*() {
const wss = new ws.Server({ port: yield* websocketPort })
const handler = applyWSSHandler({
wss,
router: yield* RPCRouter,
createContext: yield* TRPCContextCreator,
})
return { wss, handler }
}),
({ wss, handler }) => Effect.sync(() => {
handler.broadcastReconnectNotification()
wss.close()
}),
))
}

View File

@@ -1,5 +0,0 @@
import { Config } from "effect"
export const rpcRoot = Config.string("RPC_ROOT").pipe(Config.withDefault("/rpc"))
export const rpcPlaygroundRoot = Config.string("RPC_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground"))