pretty + socket separation

This commit is contained in:
Kevin Puig
2025-09-28 03:18:11 -04:00
parent 460991f2ae
commit b3e24f4493
5 changed files with 74 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ import { Server } from "socket.io";
import routes from "./routes/index"; import routes from "./routes/index";
import { Scalar } from "@scalar/hono-api-reference"; import { Scalar } from "@scalar/hono-api-reference";
import { openAPIRouteHandler } from "hono-openapi"; import { openAPIRouteHandler } from "hono-openapi";
import { registerSocketHandlers } from "./sockets";
// Routes // Routes
const app = new Hono(); const app = new Hono();
@@ -13,7 +14,11 @@ app.use(
"*", "*",
cors({ cors({
origin: "http://localhost:5173", origin: "http://localhost:5173",
allowHeaders: ["Content-Type", "Authorization", "Access-Control-Allow-Origin"], allowHeaders: [
"Content-Type",
"Authorization",
"Access-Control-Allow-Origin",
],
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
credentials: true, credentials: true,
}), }),
@@ -42,19 +47,13 @@ const io = new Server({
cors: { cors: {
origin: "http://localhost:5173", origin: "http://localhost:5173",
credentials: true, credentials: true,
} },
}); });
const engine = new Engine(); const engine = new Engine();
io.bind(engine); io.bind(engine);
// Register socket.io events // Register socket.io events
io.on("connection", (socket) => { registerSocketHandlers(io);
console.log("connected1");
socket.on("ping", (c) => {
console.log(c);
socket.emit("pong", c);
});
});
const { websocket } = engine.handler(); const { websocket } = engine.handler();
@@ -68,7 +67,10 @@ export default {
if (url.pathname === "/socket.io/") { if (url.pathname === "/socket.io/") {
const response = await engine.handleRequest(req, server); const response = await engine.handleRequest(req, server);
// Add CORS headers explicitly // Add CORS headers explicitly
response.headers.set("Access-Control-Allow-Origin", "http://localhost:5173"); response.headers.set(
"Access-Control-Allow-Origin",
"http://localhost:5173",
);
response.headers.set("Access-Control-Allow-Credentials", "true"); response.headers.set("Access-Control-Allow-Credentials", "true");
return response; return response;
} else { } else {
@@ -76,5 +78,5 @@ export default {
} }
}, },
websocket websocket,
}; };

View File

@@ -0,0 +1,16 @@
import { Server } from "socket.io";
import { registerVoiceHandlers } from "./voiceHandler";
export function registerSocketHandlers(io: Server) {
// bad practice
io.on("connection", (socket) => {
console.log("connected");
socket.on("ping", (c) => {
console.log(c);
socket.emit("pong", c);
});
});
// good practice
registerVoiceHandlers(io);
}

View File

@@ -0,0 +1,33 @@
import { Server, Socket } from "socket.io";
//TEST IGNORE
export function registerVoiceHandlers(io: Server) {
io.on("connection", (socket: Socket) => {
console.log(`Voice socket connected: ${socket.id}`);
socket.on("join-voice-channel", (channelId: string) => {
socket.join(channelId);
console.log(`Socket ${socket.id} joined voice channel ${channelId}`);
// Optionally, notify others in the channel
socket.to(channelId).emit("user-joined-voice", socket.id);
});
socket.on("leave-voice-channel", (channelId: string) => {
socket.leave(channelId);
console.log(`Socket ${socket.id} left voice channel ${channelId}`);
// Optionally, notify others in the channel
socket.to(channelId).emit("user-left-voice", socket.id);
});
socket.on("voice-data", (channelId: string, data: any) => {
// Broadcast voice data to all other clients in the same channel
socket.to(channelId).emit("voice-data", socket.id, data);
});
socket.on("disconnect", () => {
console.log(`Voice socket disconnected: ${socket.id}`);
// Handle user leaving all voice channels they were in
// (e.g., iterate through socket.rooms if you need to emit to specific channels)
});
});
}

View File

@@ -52,5 +52,5 @@ export type GetChannelsByCategoryIdInput = z.infer<
export type UpdateChannelInput = z.infer<typeof updateChannelSchema>; export type UpdateChannelInput = z.infer<typeof updateChannelSchema>;
export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>; export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>;
export type DeleteChannelsByCategoryIdInput = z.infer< export type DeleteChannelsByCategoryIdInput = z.infer<
typeof deleteChannelsByCategoryIdSchema typeof deleteChannelsByCategoryIdSchema
>; >;

View File

@@ -12,5 +12,7 @@ export const postMessageToChannelSchema = z.object({
//TODO: add more realtime related validators as needed //TODO: add more realtime related validators as needed
export type PostMessageToChannelInput = z.infer<typeof postMessageToChannelSchema>; export type PostMessageToChannelInput = z.infer<
typeof postMessageToChannelSchema
>;
//TODO: create more input schemas for other realtime actions //TODO: create more input schemas for other realtime actions