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);
// },10000); // every 10 sec
socket.on('listBud', async (myBuddies: string) => {
socket.on('listBud', async (myBuddies: string[]) => {
const buddies = document.getElementById('div-buddies') as HTMLDivElement;
console.log('%cList buddies connected ',color.yellow, 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
* @param socket
* @param buddies
* @param listBuddies
* @param bud
* @returns
*/
export async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies: string) {
export async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies: string[]) {
if (!buddies) return;
const sendtextbox = document.getElementById('t-chat-window') as HTMLButtonElement;
const buddiesElement = document.createElement("div-buddies-list");
buddiesElement.textContent = listBuddies + '\n';
const user = getUser()?.name ?? "";
buddies.appendChild(buddiesElement);
buddies.scrollTop = buddies.scrollHeight;
console.log(`Added buddies: ${listBuddies}`);
buddies.innerHTML = "";
for (const bud of listBuddies)
{
if (!buddies) return;
const sendtextbox = document.getElementById('t-chat-window') as HTMLButtonElement;
const buddiesElement = document.createElement("div-buddies-list");
buddiesElement.textContent = bud + '\n';
const user = getUser()?.name ?? "";
buddies.appendChild(buddiesElement);
buddies.scrollTop = buddies.scrollHeight;
console.log(`Added buddies: ${bud}`);
buddiesElement.style.cursor = "pointer";
buddiesElement.addEventListener("click", () => {
navigator.clipboard.writeText(listBuddies);
if (listBuddies !== user && user !== "") {
sendtextbox.value = `@${listBuddies}: `;
console.log("Copied to clipboard:", listBuddies);
sendtextbox.focus();
}
});
buddiesElement.addEventListener("dblclick", () => {
console.log("Open profile:", listBuddies);
getProfil(socket, listBuddies);
sendtextbox.value = "";
});
buddiesElement.style.cursor = "pointer";
buddiesElement.addEventListener("click", () => {
navigator.clipboard.writeText(bud);
if (bud !== user && user !== "") {
sendtextbox.value = `@${bud}: `;
console.log("Copied to clipboard:", bud);
sendtextbox.focus();
}
});
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
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
location /api/chat/ {
proxy_pass http://chat;
proxy_pass http://app-chat;
}
location /api/chat/socket.io/ {
@ -10,5 +10,5 @@ location /api/chat/socket.io/ {
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
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
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;
const seen = new Set<string>();
// <- only log/count unique usernames
const listBud: string[] = [];
for (const [socketId, username] of clientChat) {
// Basic checks
if (typeof socketId !== 'string' || socketId.length === 0) {
@ -38,11 +39,15 @@ export function connectedUser(io?: Server, target?: string): number {
// socket exists and is connected
seen.add(username.user);
count++;
const targetSocketId = target;
io.to(targetSocketId!).emit('listBud', username.user);
// const targetSocketId = target;
// io.to(targetSocketId!).emit('listBud', username.user);
listBud.push(username.user);
continue;
}
count++;
}
if (io && typeof io.sockets?.sockets?.get === 'function' && target) {
io.to(target).emit('listBud', listBud);
}
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"]
}