pretty + socket separation
This commit is contained in:
@@ -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,
|
||||||
};
|
};
|
||||||
|
|||||||
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 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
|
||||||
>;
|
>;
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
export const postMessageToChannelSchema = z.object({
|
export const postMessageToChannelSchema = z.object({
|
||||||
instanceId: z.uuidv7(),
|
instanceId: z.uuidv7(),
|
||||||
categoryId: z.uuidv7(),
|
categoryId: z.uuidv7(),
|
||||||
channelId: z.uuidv7(),
|
channelId: z.uuidv7(),
|
||||||
userId: z.uuidv7(),
|
userId: z.uuidv7(),
|
||||||
content: z.string().min(1).max(2000),
|
content: z.string().min(1).max(2000),
|
||||||
repliedMessageId: z.uuidv7().optional(),
|
repliedMessageId: z.uuidv7().optional(),
|
||||||
token: z.string(),
|
token: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
//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
|
||||||
Reference in New Issue
Block a user