pretty + socket separation
This commit is contained in:
@@ -5,6 +5,7 @@ import { Server } from "socket.io";
|
||||
import routes from "./routes/index";
|
||||
import { Scalar } from "@scalar/hono-api-reference";
|
||||
import { openAPIRouteHandler } from "hono-openapi";
|
||||
import { registerSocketHandlers } from "./sockets";
|
||||
|
||||
// Routes
|
||||
const app = new Hono();
|
||||
@@ -13,7 +14,11 @@ app.use(
|
||||
"*",
|
||||
cors({
|
||||
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"],
|
||||
credentials: true,
|
||||
}),
|
||||
@@ -42,19 +47,13 @@ const io = new Server({
|
||||
cors: {
|
||||
origin: "http://localhost:5173",
|
||||
credentials: true,
|
||||
}
|
||||
},
|
||||
});
|
||||
const engine = new Engine();
|
||||
io.bind(engine);
|
||||
|
||||
// Register socket.io events
|
||||
io.on("connection", (socket) => {
|
||||
console.log("connected1");
|
||||
socket.on("ping", (c) => {
|
||||
console.log(c);
|
||||
socket.emit("pong", c);
|
||||
});
|
||||
});
|
||||
registerSocketHandlers(io);
|
||||
|
||||
const { websocket } = engine.handler();
|
||||
|
||||
@@ -68,7 +67,10 @@ export default {
|
||||
if (url.pathname === "/socket.io/") {
|
||||
const response = await engine.handleRequest(req, server);
|
||||
// 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");
|
||||
return response;
|
||||
} else {
|
||||
@@ -76,5 +78,5 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
websocket
|
||||
websocket,
|
||||
};
|
||||
|
||||
16
concord-server/src/sockets/index.ts
Normal file
16
concord-server/src/sockets/index.ts
Normal 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);
|
||||
}
|
||||
33
concord-server/src/sockets/voiceHandler.ts
Normal file
33
concord-server/src/sockets/voiceHandler.ts
Normal 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)
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -52,5 +52,5 @@ export type GetChannelsByCategoryIdInput = z.infer<
|
||||
export type UpdateChannelInput = z.infer<typeof updateChannelSchema>;
|
||||
export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>;
|
||||
export type DeleteChannelsByCategoryIdInput = z.infer<
|
||||
typeof deleteChannelsByCategoryIdSchema
|
||||
typeof deleteChannelsByCategoryIdSchema
|
||||
>;
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const postMessageToChannelSchema = z.object({
|
||||
instanceId: z.uuidv7(),
|
||||
categoryId: z.uuidv7(),
|
||||
channelId: z.uuidv7(),
|
||||
userId: z.uuidv7(),
|
||||
content: z.string().min(1).max(2000),
|
||||
repliedMessageId: z.uuidv7().optional(),
|
||||
token: z.string(),
|
||||
instanceId: z.uuidv7(),
|
||||
categoryId: z.uuidv7(),
|
||||
channelId: z.uuidv7(),
|
||||
userId: z.uuidv7(),
|
||||
content: z.string().min(1).max(2000),
|
||||
repliedMessageId: z.uuidv7().optional(),
|
||||
token: z.string(),
|
||||
});
|
||||
|
||||
//TODO: add more realtime related validators as needed
|
||||
|
||||
export type PostMessageToChannelInput = z.infer<typeof postMessageToChannelSchema>;
|
||||
//TODO: create more input schemas for other realtime actions
|
||||
export type PostMessageToChannelInput = z.infer<
|
||||
typeof postMessageToChannelSchema
|
||||
>;
|
||||
//TODO: create more input schemas for other realtime actions
|
||||
|
||||
Reference in New Issue
Block a user