socket.io first attempt

This commit is contained in:
NigeParis 2025-11-16 19:42:19 +01:00 committed by Maix0
parent 5a905a1239
commit cf6f3145b6
9 changed files with 4901 additions and 7 deletions

View file

@ -27,7 +27,8 @@
"@sinclair/typebox": "^0.34.41",
"fastify": "^5.6.2",
"fastify-cli": "^7.4.1",
"fastify-plugin": "^5.1.0"
"fastify-plugin": "^5.1.0",
"socket.io": "^4.8.1"
},
"devDependencies": {
"@types/node": "^22.19.1",

View file

@ -2,6 +2,57 @@ import { FastifyPluginAsync } from 'fastify';
import { MakeStaticResponse, typeResponse } from '@shared/utils';
import { Type } from '@sinclair/typebox';
import Fastify from 'fastify'
import { Server } from "socket.io"
// import path from 'path'
// import fastifyStatic from '@fastify/static'
// import fs from 'fs/promises'
// import { fileURLToPath } from 'url';
// import { dirname } from 'path';
// const __filename = fileURLToPath(import.meta.url);
// const __dirname = dirname(__filename);
// const fastifyWS = Fastify({
// logger: true
// })
// fastify.register(fastifyStatic, {
// root: path.join(__dirname, ''), prefix: '/'
// })
// const io = new Server(8888, {
// cors: {
// origin: "*",
// }
// })
const fastify = Fastify();
const io = new Server(fastify.server, { cors: { origin: "*" } });
import { Socket } from "socket.io";
io.on("connection", (socket: Socket) => {
console.log("testing")
console.log(`Client connected: ${socket.id}`);
socket.on("message", (data: any) => console.log(data, `socketID: ${socket.id}`));
socket.once("message", () => socket.send("connected succesfully"));
socket.once("coucou", (data: any) => console.log(data))
});
export const ChatRes = {
200: typeResponse('success', 'chat.success', {
name: Type.String(),