Skip to content

Commit d02b86e

Browse files
authored
Merge pull request #4 from VerityIncorporated/main
Added disconnect client to the Websocket server
2 parents 9b5a457 + e0fa603 commit d02b86e

5 files changed

Lines changed: 85 additions & 3 deletions

File tree

scripting/include/websocket/ws.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,22 @@ methodmap WebSocketServer < Handle
302302
*/
303303
public native bool SendMessageToClient(const char[] clientId, const char[] message);
304304

305+
/**
306+
* Forcibly disconnect client from websocket
307+
*
308+
* @param clientId client id
309+
*/
310+
public native bool DisconnectClient(const char[] clientId);
311+
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+
305321
/**
306322
* Start the WebSocket server
307323
*/

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: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ static cell_t ws_SendMessageToClient(IPluginContext *pContext, const cell_t *par
171171
return pWebsocketServer->sendToClient(clientId, msg);
172172
}
173173

174+
static cell_t ws_DisconnectClient(IPluginContext *pContext, const cell_t *params)
175+
{
176+
WebSocketServer* pWebsocketServer = GetWsServerPointer(pContext, params[1]);
177+
178+
if (!pWebsocketServer)
179+
{
180+
return 0;
181+
}
182+
183+
char *clientId;
184+
pContext->LocalToString(params[2], &clientId);
185+
186+
return pWebsocketServer->disconnectClient(clientId);
187+
}
188+
174189
static cell_t ws_BroadcastMessage(IPluginContext *pContext, const cell_t *params)
175190
{
176191
WebSocketServer* pWebsocketServer = GetWsServerPointer(pContext, params[1]);
@@ -250,8 +265,44 @@ static cell_t ws_IsDeflateEnabled(IPluginContext *pContext, const cell_t *params
250265

251266
static cell_t ws_GetClients(IPluginContext *pContext, const cell_t *params)
252267
{
253-
// TODO: Implement
254-
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;
255306
}
256307

257308
const sp_nativeinfo_t ws_natives_server[] =
@@ -265,6 +316,7 @@ const sp_nativeinfo_t ws_natives_server[] =
265316
{"WebSocketServer.Stop", ws_Stop},
266317
{"WebSocketServer.BroadcastMessage", ws_BroadcastMessage},
267318
{"WebSocketServer.SendMessageToClient", ws_SendMessageToClient},
319+
{"WebSocketServer.DisconnectClient", ws_DisconnectClient},
268320
{"WebSocketServer.ClientsCount.get", ws_GetClientsCount},
269321
{"WebSocketServer.EnablePong.get", ws_SetOrGetPongEnable},
270322
{"WebSocketServer.EnablePong.set", ws_SetOrGetPongEnable},

src/ws_server.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,17 @@ bool WebSocketServer::sendToClient(const std::string& clientId, const std::strin
146146
}
147147
}
148148
return false;
149+
}
150+
151+
bool WebSocketServer::disconnectClient(const std::string& clientId) {
152+
auto clients = m_webSocketServer.getClients();
153+
154+
for (const auto& client : clients)
155+
{
156+
if (client.second == clientId) {
157+
client.first->stop();
158+
return true;
159+
}
160+
}
161+
return false;
149162
}

src/ws_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class WebSocketServer
1414
void OnError(ix::WebSocketErrorInfo errorInfo, std::shared_ptr<ix::ConnectionState> connectionState);
1515
void broadcastMessage(const std::string& message);
1616
bool sendToClient(const std::string& clientId, const std::string& message);
17+
bool disconnectClient(const std::string& clientId);
1718

1819
ix::WebSocketServer m_webSocketServer;
1920
Handle_t m_webSocketServer_handle = BAD_HANDLE;

0 commit comments

Comments
 (0)