Skip to content

Commit e0fa603

Browse files
committed
Implement GetClients and bump SMEXT_CONF_VERSION to 1.0.4
1 parent dc16934 commit e0fa603

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

scripting/include/websocket/ws.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,15 @@ methodmap WebSocketServer < Handle
309309
*/
310310
public native bool DisconnectClient(const char[] clientId);
311311

312+
/**
313+
* Get handles for all connected clients
314+
*
315+
* @param buffer Array to store client handles
316+
* @param maxSize Maximum size of the array
317+
* @return Number of handles stored
318+
*/
319+
public native int GetClients(WebSocket[] buffer, int maxSize);
320+
312321
/**
313322
* Start the WebSocket server
314323
*/

src/smsdk_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#define SMEXT_CONF_NAME "SourceMod WebSocket Extension"
55
#define SMEXT_CONF_DESCRIPTION "Provide JSON and WebSocket Native"
6-
#define SMEXT_CONF_VERSION "1.0.3"
6+
#define SMEXT_CONF_VERSION "1.0.4"
77
#define SMEXT_CONF_AUTHOR "ProjectSky"
88
#define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-websocket"
99
#define SMEXT_CONF_LOGTAG "websocket"

src/ws_natives_server.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,44 @@ static cell_t ws_IsDeflateEnabled(IPluginContext *pContext, const cell_t *params
265265

266266
static cell_t ws_GetClients(IPluginContext *pContext, const cell_t *params)
267267
{
268-
// TODO: Implement
269-
return 1;
268+
WebSocketServer *pWebsocketServer = GetWsServerPointer(pContext, params[1]);
269+
270+
if (!pWebsocketServer)
271+
{
272+
return 0;
273+
}
274+
275+
cell_t *outputArray;
276+
pContext->LocalToPhysAddr(params[2], &outputArray);
277+
cell_t maxSize = params[3];
278+
279+
auto clients = pWebsocketServer->m_webSocketServer.getClients();
280+
size_t count = 0;
281+
282+
HandleSecurity sec(nullptr, myself->GetIdentity());
283+
284+
for (const auto &client : clients)
285+
{
286+
if (count >= maxSize)
287+
break;
288+
289+
WebSocketClient *pClient = new WebSocketClient(client.first.get());
290+
HandleError err;
291+
Handle_t handle = handlesys->CreateHandleEx(g_htWsClient, pClient, &sec, nullptr, &err);
292+
293+
if (handle != BAD_HANDLE)
294+
{
295+
pClient->m_websocket_handle = handle;
296+
pClient->m_keepConnecting = true;
297+
outputArray[count++] = handle;
298+
}
299+
else
300+
{
301+
delete pClient;
302+
}
303+
}
304+
305+
return count;
270306
}
271307

272308
const sp_nativeinfo_t ws_natives_server[] =

0 commit comments

Comments
 (0)