WIP BroadCast working on console... needs more work
This commit is contained in:
parent
d89c21dd2c
commit
98631be918
13 changed files with 467 additions and 871 deletions
|
|
@ -7,125 +7,7 @@
|
|||
"components": {
|
||||
"schemas": {}
|
||||
},
|
||||
"paths": {
|
||||
"/api/chat/socket.io": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Default Response"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/chat/test": {
|
||||
"get": {
|
||||
"operationId": "chatTest",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Default Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"kind",
|
||||
"msg",
|
||||
"payload"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"enum": [
|
||||
"success"
|
||||
]
|
||||
},
|
||||
"msg": {
|
||||
"enum": [
|
||||
"chat.success"
|
||||
]
|
||||
},
|
||||
"payload": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"name",
|
||||
"id",
|
||||
"guest"
|
||||
],
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"guest": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "Default Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"kind",
|
||||
"msg"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"enum": [
|
||||
"notLoggedIn"
|
||||
]
|
||||
},
|
||||
"msg": {
|
||||
"enum": [
|
||||
"auth.noCookie",
|
||||
"auth.invalidKind",
|
||||
"auth.noUser",
|
||||
"auth.invalid"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"kind",
|
||||
"msg"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"enum": [
|
||||
"notLoggedIn"
|
||||
]
|
||||
},
|
||||
"msg": {
|
||||
"enum": [
|
||||
"auth.noCookie",
|
||||
"auth.invalidKind",
|
||||
"auth.noUser",
|
||||
"auth.invalid"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"paths": {},
|
||||
"servers": [
|
||||
{
|
||||
"url": "https://local.maix.me:8888",
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
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 { Socket } from 'socket.io';
|
||||
import * as fsocketio from 'fastify-socket.io';
|
||||
|
||||
|
||||
|
|
@ -28,31 +25,22 @@ export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
|||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
await fastify.register(fsocketio.default);
|
||||
|
||||
// fastify.get('/api/chat/socket.io', (req, reply) => {
|
||||
|
||||
// console.log('Socket.io endpoint hit');
|
||||
// reply.send({ status: 'ok' });
|
||||
|
||||
// });
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fastify.get(
|
||||
'/api/chat/test',
|
||||
{
|
||||
schema: {
|
||||
response: ChatRes,
|
||||
operationId: 'chatTest',
|
||||
},
|
||||
config: { requireAuth: true },
|
||||
},
|
||||
async (req, res) => {
|
||||
console.log('/api/chat called =================>');
|
||||
res.makeResponse(200, 'success', 'chat.success', { name: req.authUser!.name, 'id': req.authUser!.id, guest: false });
|
||||
},
|
||||
);
|
||||
// fastify.get(
|
||||
// '/api/chat/test',
|
||||
// {
|
||||
// schema: {
|
||||
// response: ChatRes,
|
||||
// operationId: 'chatTest',
|
||||
// },
|
||||
// config: { requireAuth: true },
|
||||
// },
|
||||
// async (req, res) => {
|
||||
// console.log('/api/chat called =================>');
|
||||
// res.makeResponse(200, 'success', 'chat.success', { name: req.authUser!.name, 'id': req.authUser!.id, guest: false });
|
||||
// },
|
||||
// );
|
||||
};
|
||||
export default route;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,11 @@
|
|||
import { Server } from 'socket.io';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
export const color = {
|
||||
red: "\x1b[31m",
|
||||
green: "\x1b[32m",
|
||||
yellow: "\x1b[33m",
|
||||
blue: "\x1b[34m",
|
||||
reset: "\x1b[0m",
|
||||
};
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
|
|
@ -7,29 +14,84 @@ declare module 'fastify' {
|
|||
hello: (message: string) => string,
|
||||
MsgObjectServer: (data: { message: string }) => void,
|
||||
message: (msg: string) => void,
|
||||
testend: (sock_id_client: string) => void,
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export function setupSocketIo(fastify: import('fastify').FastifyInstance): void {
|
||||
|
||||
fastify.ready((err) => {
|
||||
|
||||
fastify.ready((err) => {
|
||||
if (err) throw err;
|
||||
|
||||
fastify.io.on('connection', (socket) => {
|
||||
console.info('Socket connected!', socket.id);
|
||||
socket.on('hello', (value) => {
|
||||
return 'hi';
|
||||
|
||||
function broadcast(message: any, sender?: any) {
|
||||
|
||||
fastify.io.fetchSockets().then((sockets) => {
|
||||
console.log("Connected clients:", sockets.length);
|
||||
for (const s of sockets) {
|
||||
|
||||
if (s.id !== sender) {
|
||||
s.emit('MsgObjectServer',{ message: `${message}` });
|
||||
console.log("Socket ID:", s.id);
|
||||
console.log("Rooms:", [...s.rooms]);
|
||||
console.log("Sender:", sender ? sender : 'none');
|
||||
}
|
||||
}
|
||||
});
|
||||
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}`));
|
||||
},);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(fastify.io)));
|
||||
fastify.io.on('connection', (socket : Socket) => {
|
||||
console.info('Socket connected!', socket.id);
|
||||
// socket.on('hello', (value) => {
|
||||
// return 'hi';
|
||||
// });
|
||||
|
||||
// fastify.io.fetchSockets().then((sockets) => {
|
||||
// console.log("Connected clients:", sockets.length);
|
||||
// for (const s of sockets) {
|
||||
// console.log("Socket ID:", s.id);
|
||||
// console.log("Rooms:", [...s.rooms]);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
|
||||
// socket.emit("MsgObjectServer", {message: `THIS IS A SERVER MESSAGE`});
|
||||
socket.on('message', (message: string) => {
|
||||
// console.log(color.red + `GOT MESSAGE ${color.reset} ${value}`);
|
||||
|
||||
console.log(color.green + `GOT MESSAGE from client ${socket.id}: ${color.reset} ${message}`);
|
||||
const obj = JSON.parse(message);
|
||||
const userID = obj.userID;
|
||||
console.log(`Message from client ${obj.userID}: ${obj.text}`);
|
||||
broadcast(`Broadcast from THIS server: ${obj.text}`,userID);
|
||||
});
|
||||
// socket.on('MsgObjectServer', (value) => { console.log(`GOT COUCOU ${value.message}`)
|
||||
//broadcast(`Broadcast from server:`, socket.id );
|
||||
// });
|
||||
|
||||
socket.on('testend', (sock_id_cl : string) => {
|
||||
console.log('testend received from client socket id:', sock_id_cl);
|
||||
});
|
||||
socket.on('disconnecting', (reason ) => {
|
||||
console.log("Client is disconnecting:", socket.id, "reason:", reason);
|
||||
console.log('Socket AAAAAAAActing because:', socket.connected);
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
// fastify.io.on('disconnect', (socket : Socket) => {
|
||||
// console.log('weeeeeeeeeeeewoooooooooooooooooooooooooo');
|
||||
// console.log('alert in the high castle');
|
||||
// console.log('Socket disconnected!', socket.id);
|
||||
// });
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue