separated socket from app for clear develoopement
This commit is contained in:
parent
0a504a75ce
commit
9889600708
8 changed files with 129 additions and 52 deletions
|
|
@ -8,6 +8,15 @@
|
|||
"schemas": {}
|
||||
},
|
||||
"paths": {
|
||||
"/api/chat/socket.io": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Default Response"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/chat/test": {
|
||||
"get": {
|
||||
"operationId": "chatTest",
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ import * as db from '@shared/database';
|
|||
import * as auth from '@shared/auth';
|
||||
import * as swagger from '@shared/swagger';
|
||||
import * as utils from '@shared/utils';
|
||||
import { Server } from 'socket.io';
|
||||
import useSocketIo from 'fastify-socket.io';
|
||||
import { setupSocketIo } from "./socket";
|
||||
|
||||
declare const __SERVICE_NAME: string;
|
||||
|
||||
|
|
@ -15,17 +15,6 @@ const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
|
|||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
io: Server<{
|
||||
hello: (message: string) => string,
|
||||
coucou: (data: { message: string }) => void,
|
||||
message: (msg: string) => void,
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
||||
void opts;
|
||||
await fastify.register(utils.useMakeResponse);
|
||||
|
|
@ -49,21 +38,9 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
|||
void fastify.register(fastifyMultipart, {});
|
||||
fastify.get('/monitoring', () => 'Ok');
|
||||
|
||||
fastify.ready((err) => {
|
||||
if (err) throw err;
|
||||
|
||||
fastify.io.on('connection', (socket) => {
|
||||
console.info('Socket connected!', socket.id);
|
||||
socket.on('hello', (value) => {
|
||||
console.log(`GOT HELLO ${value}`);
|
||||
return 'hi';
|
||||
});
|
||||
socket.on('message', (value) => console.log(`GOT MESSAGE ${value}`));
|
||||
socket.on('coucou', (value) => console.log(`GOT COUCOU ${value.message}`));
|
||||
},
|
||||
);
|
||||
|
||||
});
|
||||
// Setup Socket.io
|
||||
setupSocketIo(fastify);
|
||||
|
||||
};
|
||||
|
||||
export default app;
|
||||
|
|
|
|||
|
|
@ -7,18 +7,12 @@ import { Socket } from 'socket.io';
|
|||
import * as fsocketio from 'fastify-socket.io';
|
||||
|
||||
|
||||
const fastify = Fastify();
|
||||
// const fastify = Fastify();
|
||||
|
||||
const io = new Server(fastify.server, {
|
||||
path: '/app/chat/socket.io/',
|
||||
cors: { origin: '*' },
|
||||
});
|
||||
|
||||
|
||||
io.on('connection', (socket: Socket) => {
|
||||
console.log('testing');
|
||||
console.log(`Client connected: ${socket.id}`);
|
||||
});
|
||||
// const io = new Server(fastify.server, {
|
||||
// path: '/app/chat/socket.io/',
|
||||
// cors: { origin: '*' },
|
||||
// });
|
||||
|
||||
|
||||
export const ChatRes = {
|
||||
|
|
@ -33,18 +27,17 @@ export const ChatRes = {
|
|||
export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
/* await fastify.register(fsocketio.default);
|
||||
await fastify.register(fsocketio.default);
|
||||
|
||||
// fastify.get('/api/chat/socket.io', (req, reply) => {
|
||||
|
||||
fastify.get('/api/chat/socket.io', (req, reply) => {
|
||||
console.log('GOT SOCKET ?!');
|
||||
// console.log('Socket.io endpoint hit');
|
||||
// reply.send({ status: 'ok' });
|
||||
|
||||
const socket = (fastify as any).io;
|
||||
socket.emit('hello');
|
||||
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));
|
||||
});
|
||||
*/
|
||||
// });
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fastify.get(
|
||||
|
|
|
|||
35
src/chat/src/socket.ts
Normal file
35
src/chat/src/socket.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Server } from 'socket.io';
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
io: Server<{
|
||||
hello: (message: string) => string,
|
||||
MsgObjectServer: (data: { message: string }) => void,
|
||||
message: (msg: string) => void,
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
export function setupSocketIo(fastify: import('fastify').FastifyInstance): void {
|
||||
|
||||
fastify.ready((err) => {
|
||||
if (err) throw err;
|
||||
|
||||
fastify.io.on('connection', (socket) => {
|
||||
console.info('Socket connected!', socket.id);
|
||||
socket.on('hello', (value) => {
|
||||
return 'hi';
|
||||
});
|
||||
socket.emit("MsgObjectServer", {message: `THIS IS A SERVER MESSAGE`});
|
||||
socket.on('message', (value) => console.log(`GOT MESssSAGE ${value}`));
|
||||
socket.on('MsgObjectServer', (value) => console.log(`GOT COUCOU ${value.message}`));
|
||||
},);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1190,6 +1190,18 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
"/api/chat/socket.io": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Default Response"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
"openapi_other"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/chat/test": {
|
||||
"get": {
|
||||
"operationId": "chatTest",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue