Added Connected button to show how many connected to the chat
This commit is contained in:
parent
e530de6ce7
commit
b215904cfd
3 changed files with 220 additions and 213 deletions
|
|
@ -6,7 +6,6 @@ import * as auth from '@shared/auth';
|
|||
import * as swagger from '@shared/swagger';
|
||||
import * as utils from '@shared/utils';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
import { Null } from '@sinclair/typebox';
|
||||
|
||||
declare const __SERVICE_NAME: string;
|
||||
|
||||
|
|
@ -27,10 +26,7 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
|||
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
|
||||
// await fastify.register(useSocketIo, {
|
||||
// path: '/api/chat/socket.io',
|
||||
// });
|
||||
|
||||
|
||||
// Place here your custom code!
|
||||
for (const plugin of Object.values(plugins)) {
|
||||
void fastify.register(plugin as FastifyPluginAsync, {});
|
||||
|
|
@ -79,51 +75,56 @@ declare module 'fastify' {
|
|||
async function onReady(fastify: FastifyInstance) {
|
||||
|
||||
|
||||
function connectedUser(io?: Server): number {
|
||||
let count = 0;
|
||||
const seen = new Set<string>(); // <- only log/count unique usernames
|
||||
function connectedUser(io?: Server, target?: string): number {
|
||||
let count = 0;
|
||||
const seen = new Set<string>(); // <- only log/count unique usernames
|
||||
|
||||
for (const [socketId, username] of clientChat) {
|
||||
// Basic sanity checks
|
||||
if (typeof socketId !== "string" || socketId.length === 0) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
if (typeof username !== "string" || username.length === 0) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
for (const [socketId, username] of clientChat) {
|
||||
// Basic sanity checks
|
||||
if (typeof socketId !== "string" || socketId.length === 0) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
if (typeof username !== "string" || username.length === 0) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we have the io instance, attempt to validate the socket is still connected
|
||||
if (io && typeof io.sockets?.sockets?.get === "function") {
|
||||
const s = io.sockets.sockets.get(socketId) as Socket | undefined;
|
||||
// If socket not found or disconnected, remove from map and skip
|
||||
if (!s || s.disconnected) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip duplicates (DO NOT delete them — just don't count)
|
||||
if (seen.has(username)) {
|
||||
continue;
|
||||
}
|
||||
// socket exists and is connected
|
||||
seen.add(username);
|
||||
count++;
|
||||
// console.log(color.green,"count: ", count);
|
||||
console.log(color.yellow, "Client:", color.reset, username);
|
||||
console.log(color.yellow, "Chat Socket ID:", color.reset, socketId);
|
||||
continue;
|
||||
}
|
||||
// If we have the io instance, attempt to validate the socket is still connected
|
||||
if (io && typeof io.sockets?.sockets?.get === "function") {
|
||||
const s = io.sockets.sockets.get(socketId) as Socket | undefined;
|
||||
// If socket not found or disconnected, remove from map and skip
|
||||
if (!s || s.disconnected) {
|
||||
clientChat.delete(socketId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip duplicates (DO NOT delete them — just don't count)
|
||||
if (seen.has(username)) {
|
||||
continue;
|
||||
}
|
||||
// socket exists and is connected
|
||||
seen.add(username);
|
||||
count++;
|
||||
// console.log(color.green,"count: ", count);
|
||||
console.log(color.yellow, "Client:", color.reset, username);
|
||||
|
||||
// If no io provided, assume entries in the map are valid and count them.
|
||||
count++;
|
||||
console.log(color.red, "Client (unverified):", color.reset, username);
|
||||
console.log(color.red, "Chat Socket ID (unverified):", color.reset, socketId);
|
||||
}
|
||||
const targetSocketId: any = target;
|
||||
io.to(targetSocketId).emit("listObj", username);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
console.log(color.yellow, "Chat Socket ID:", color.reset, socketId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If no io provided, assume entries in the map are valid and count them.
|
||||
count++;
|
||||
console.log(color.red, "Client (unverified):", color.reset, username);
|
||||
console.log(color.red, "Chat Socket ID (unverified):", color.reset, socketId);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
function broadcast(data: ClientMessage, sender?: string) {
|
||||
|
|
@ -131,8 +132,8 @@ function connectedUser(io?: Server): number {
|
|||
|
||||
for (const s of sockets) {
|
||||
if (s.id !== sender) {
|
||||
// Send REAL JSON object
|
||||
|
||||
|
||||
// Send REAL JSON object
|
||||
const clientName = clientChat.get(s.id) || null;
|
||||
if (clientName !== null) {
|
||||
s.emit('MsgObjectServer', { message: data });
|
||||
|
|
@ -144,6 +145,7 @@ function connectedUser(io?: Server): number {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
fastify.io.on('connection', (socket: Socket) => {
|
||||
socket.on('message', (message: string) => {
|
||||
console.info(color.blue, 'Socket connected!', color.reset, socket.id);
|
||||
|
|
@ -153,6 +155,8 @@ function connectedUser(io?: Server): number {
|
|||
color.reset,
|
||||
message,
|
||||
);
|
||||
|
||||
|
||||
const obj: ClientMessage = JSON.parse(message) as ClientMessage;
|
||||
clientChat.set(socket.id, obj.user);
|
||||
console.log(
|
||||
|
|
@ -171,10 +175,23 @@ function connectedUser(io?: Server): number {
|
|||
console.log('testend received from client socket id:', sock_id_cl);
|
||||
});
|
||||
|
||||
|
||||
socket.on('list', () => {
|
||||
console.log(color.red, 'list activated', color.reset, socket.id);
|
||||
connectedUser(fastify.io, socket.id);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
socket.on("disconnecting", (reason) => {
|
||||
|
||||
const clientName = clientChat.get(socket.id) || null;
|
||||
console.log(color.green, `Client disconnecting: ${clientName} (${socket.id}) reason:`, reason);
|
||||
if (reason === 'transport error') return;
|
||||
|
||||
|
||||
if (clientName !== null) {
|
||||
const obj = {
|
||||
type: "chat",
|
||||
|
|
@ -186,8 +203,7 @@ function connectedUser(io?: Server): number {
|
|||
};
|
||||
|
||||
broadcast(obj, obj.SenderWindowID);
|
||||
console.log(`Client disconnecting: ${clientName} (${socket.id}) reason:`, reason);
|
||||
clientChat.delete(obj.user);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue