Skip to content

Instantly share code, notes, and snippets.

@titouancreach
Created July 9, 2024 13:36
Show Gist options
  • Save titouancreach/383f3e709acc1930b6ba2039000e2b9a to your computer and use it in GitHub Desktop.
Save titouancreach/383f3e709acc1930b6ba2039000e2b9a to your computer and use it in GitHub Desktop.
return yield* pipe(
// out
Stream.fromQueue(queue),
Stream.ensuring(Effect.zip(Queue.shutdown(queue), Console.log("Ending")), // Here I would be able to remove the "current" client from the current map of client
Stream.pipeThroughChannel(Socket.toChannel(socket)),
// in
Stream.decodeText(),
Stream.runForEach((message) =>
Effect.gen(function* () {
const parseMessage = Schema.decodeUnknown(
Schema.parseJson(InboundMessageType),
);
const parsedMessage = yield* parseMessage(message);
const eff = Match.value(parsedMessage).pipe(
Match.tag("Register", (message) => { // <---- this is my startup message
return Ref.update(connectionStore, (store) =>
HashMap.set(store, message.clientId, {
timeConnected: Date.now(),
send: queue,
reference: message.reference,
}),
);
}),
Match.orElse(() => {
return Console.log("Unknown message", parsedMessage);
}),
);
return yield* eff.pipe(Effect.tap(() => Effect.log(message)));
}),
),
Effect.annotateLogs("ws", "recv"),
Effect.as(HttpServerResponse.empty()),
);
}),
),
HttpServer.serve(HttpMiddleware.logger),
HttpServer.withLogAddress,
Layer.provide(ServerLive),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment