From f5aacb29f5f238ae9121bf43d96ad2eba361e7b7 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Wed, 18 Jun 2025 11:21:15 +0800 Subject: API(server/clients): callback fucntion for kick function --- include/libsock/server/client.hpp | 2 +- src/server/clients.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/libsock/server/client.hpp b/include/libsock/server/client.hpp index 3b0d38d..59870ce 100644 --- a/include/libsock/server/client.hpp +++ b/include/libsock/server/client.hpp @@ -85,7 +85,7 @@ class Clients { WP newClient(std::optional)>> cb = std::nullopt); void broadcast(const std::string &msg, std::optional> self = std::nullopt); // second param only specified when we want to exclude the sender - void kick(WP client, const bool kill = false, const std::string &reason = ""); + void kick(WP client, const bool kill = false, const std::string &reason = "", std::optional>>::iterator &)>> cb = std::nullopt); void addClient(const Client &client); bool nameExists(const std::string &name); diff --git a/src/server/clients.cpp b/src/server/clients.cpp index d3a9908..1a11ed7 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -99,7 +99,7 @@ std::vector Clients::getDatas() const { return m_vDatas; } -void Clients::kick(WP clientWeak, const bool kill, const std::string &reason) { +void Clients::kick(WP clientWeak, const bool kill, const std::string &reason, std::optional>>::iterator &)>> cb) { auto client = clientWeak.lock(); if (!client) return; @@ -108,9 +108,9 @@ void Clients::kick(WP clientWeak, const bool kill, const std::string &re if (it->second == client) { if (client && !client->getName().empty()) // this only exist when the client is registered - // not sure why the second is null, but m_name definately is since it's setted during register - // but we don't need to notify people if the client didn't even "join"/register anyways - broadcast(std::format("{} has left the chat", client->getName()), clientWeak); + // not sure why the second is null + if (cb) + (*cb)(it); if (!reason.empty()) client->write(reason); -- cgit v1.2.3