feat(chat): send online user in a single socketio message

This commit is contained in:
Maieul BOYER 2025-12-18 13:51:06 +01:00 committed by Maix0
parent 41fdbfbaaa
commit bcba86ed8a
9 changed files with 42 additions and 78 deletions

View file

@ -466,7 +466,7 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
// //connected(socket); // //connected(socket);
// },10000); // every 10 sec // },10000); // every 10 sec
socket.on('listBud', async (myBuddies: string) => { socket.on('listBud', async (myBuddies: string[]) => {
const buddies = document.getElementById('div-buddies') as HTMLDivElement; const buddies = document.getElementById('div-buddies') as HTMLDivElement;
console.log('%cList buddies connected ',color.yellow, myBuddies); console.log('%cList buddies connected ',color.yellow, myBuddies);
listBuddies(socket, buddies, myBuddies); listBuddies(socket, buddies, myBuddies);
@ -610,4 +610,4 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
} }
} }
}; };
addRoute('/chat', handleChat, { bypass_auth: true }); addRoute('/chat', handleChat, { bypass_auth: true });

View file

@ -11,35 +11,38 @@ import { getProfil } from './getProfil';
* collected in the clipBoard * collected in the clipBoard
* @param socket * @param socket
* @param buddies * @param buddies
* @param listBuddies * @param bud
* @returns * @returns
*/ */
export async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies: string) { export async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies: string[]) {
if (!buddies) return; buddies.innerHTML = "";
const sendtextbox = document.getElementById('t-chat-window') as HTMLButtonElement; for (const bud of listBuddies)
const buddiesElement = document.createElement("div-buddies-list"); {
buddiesElement.textContent = listBuddies + '\n'; if (!buddies) return;
const user = getUser()?.name ?? ""; const sendtextbox = document.getElementById('t-chat-window') as HTMLButtonElement;
buddies.appendChild(buddiesElement); const buddiesElement = document.createElement("div-buddies-list");
buddies.scrollTop = buddies.scrollHeight; buddiesElement.textContent = bud + '\n';
console.log(`Added buddies: ${listBuddies}`); const user = getUser()?.name ?? "";
buddies.appendChild(buddiesElement);
buddies.scrollTop = buddies.scrollHeight;
console.log(`Added buddies: ${bud}`);
buddiesElement.style.cursor = "pointer"; buddiesElement.style.cursor = "pointer";
buddiesElement.addEventListener("click", () => { buddiesElement.addEventListener("click", () => {
navigator.clipboard.writeText(listBuddies); navigator.clipboard.writeText(bud);
if (listBuddies !== user && user !== "") { if (bud !== user && user !== "") {
sendtextbox.value = `@${listBuddies}: `; sendtextbox.value = `@${bud}: `;
console.log("Copied to clipboard:", listBuddies); console.log("Copied to clipboard:", bud);
sendtextbox.focus(); sendtextbox.focus();
} }
}); });
buddiesElement.addEventListener("dblclick", () => {
console.log("Open profile:", listBuddies);
getProfil(socket, listBuddies);
sendtextbox.value = "";
});
buddiesElement.addEventListener("dblclick", () => {
console.log("Open profile:", bud);
getProfil(socket, bud);
sendtextbox.value = "";
});
}
} }

View file

@ -1,4 +1,4 @@
#forward the post request to the microservice #forward the post request to the microservice
location /api/auth/ { location /api/auth/ {
proxy_pass http://auth; proxy_pass http://app-auth;
} }

View file

@ -1,6 +1,6 @@
#forward the post request to the microservice #forward the post request to the microservice
location /api/chat/ { location /api/chat/ {
proxy_pass http://chat; proxy_pass http://app-chat;
} }
location /api/chat/socket.io/ { location /api/chat/socket.io/ {
@ -10,5 +10,5 @@ location /api/chat/socket.io/ {
proxy_set_header Connection "Upgrade"; proxy_set_header Connection "Upgrade";
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_read_timeout 3600s; proxy_read_timeout 3600s;
proxy_pass http://chat; proxy_pass http://app-chat;
} }

View file

@ -1,4 +1,4 @@
#forward the post request to the microservice #forward the post request to the microservice
location /api/user/ { location /api/user/ {
proxy_pass http://user; proxy_pass http://app-user;
} }

View file

@ -13,6 +13,7 @@ export function connectedUser(io?: Server, target?: string): number {
let count = 0; let count = 0;
const seen = new Set<string>(); const seen = new Set<string>();
// <- only log/count unique usernames // <- only log/count unique usernames
const listBud: string[] = [];
for (const [socketId, username] of clientChat) { for (const [socketId, username] of clientChat) {
// Basic checks // Basic checks
if (typeof socketId !== 'string' || socketId.length === 0) { if (typeof socketId !== 'string' || socketId.length === 0) {
@ -38,11 +39,15 @@ export function connectedUser(io?: Server, target?: string): number {
// socket exists and is connected // socket exists and is connected
seen.add(username.user); seen.add(username.user);
count++; count++;
const targetSocketId = target; // const targetSocketId = target;
io.to(targetSocketId!).emit('listBud', username.user); // io.to(targetSocketId!).emit('listBud', username.user);
listBud.push(username.user);
continue; continue;
} }
count++; count++;
} }
if (io && typeof io.sockets?.sockets?.get === 'function' && target) {
io.to(target).emit('listBud', listBud);
}
return count; return count;
} }

View file

@ -1,8 +0,0 @@
#!/bin/sh
set -e
set -x
# do anything here
# run the CMD [ ... ] from the dockerfile
exec "$@"

View file

@ -1,21 +0,0 @@
{
"openapi": "3.1.0",
"info": {
"version": "9.6.1",
"title": "@fastify/swagger"
},
"components": {
"schemas": {}
},
"paths": {},
"servers": [
{
"url": "https://local.maix.me:8888",
"description": "direct from docker"
},
{
"url": "https://local.maix.me:8000",
"description": "using fnginx"
}
]
}

View file

@ -1,15 +0,0 @@
// {
// "extends": "../tsconfig.base.json",
// "compilerOptions": {
// "skipLibCheck": true, // skips type checking for all .d.ts files
// "moduleResolution": "node",
// "esModuleInterop": true,
// "types": ["node"] },
// "include": ["src/**/*.ts"]
// }
{
"extends": "../tsconfig.base.json",
"compilerOptions": {},
"include": ["src/**/*.ts"]
}