A sourcemod native extension.
Invoke custom events on your soucemod server with custom data via websockets as JSON (or raw TCP) and listen to them from source pawn.
Uses boost beast, boost asio and openssl.
Connect your websocket client to the configured port (443 or 80). Invoke events by sending JSON to the server. Currently only one client is supported at once.
Events are sent as JSON. The event name is required as event alongwith any arguments as an object args.
int, bool, float, string, objects and arrays are supported as arguments. Example:
{
"event": "myEventName",
"args": {
"someInt": 5,
"name": "rob5300",
"subObject": {
"numbers": [1,2,3]
}
},
"token": "mytoken123"
}
If enabled you must also provide a valid token as token (these are user configured in the extension config).
The websocket server will reply with a message to indicate success or failiure of parsing and processing the last event message in the format:
{
"success": false,
"message": "Some error message"
}
Use AddEventListener() to subscribe to an event with a specific name. The EventArgs object argument contains the data from the event payload similarly to KeyValues.
The event args object has many functions to get your data as a string, int, bool and float. Arrays are also supported. If you have objects within objects then these can be retrieved as EventArg objects. Similarly to KeyValues, you can specify a path as a key to get values quickly from nested objects.
EventArgs objects must be disposed of when no longer used, these are Handles.
Example of listening to an event in sourcepawn and getting data:
public void OnPluginStart()
{
AddEventListener("my_event", EventCallback1);
}
void EventCallback1(EventArgs args)
{
char value[256];
if(args.GetString("string", value, 256))
{
LogAction(0, -1, "string: %s", value);
}
if(args.GetString("player/name", value, 256))
{
LogAction(0, -1, "Player name: %s", value);
}
int num = args.GetInt("int");
IntToString(num, value, 256);
LogAction(0, -1, "int: %i", num);
delete args;
}
Check tcp_readme.md for info on how to use raw TCP mode.
Configuration is read from sourcemod/configs/socketevents.json and is JSON. Below is a sample of the config:
{
"port": 443, //Port to bind and listen to
"websockets": true, //If websockets should be used
"eventsPerFrame": 4, //How many events max per game tick to send to listeners
"tokens": [
"mytoken123"
]
}
Ensure you have the required dependencies for building Sourcemod. Ambuild is not used by this project.
Metamod, Sourcemod and hl2sdk-tf2 are needed. These need to be in the same directory as this project (../sourcemod, ../hl2sdk-tf2, ../mmsource-1.10)
Install/update g++ gcc g++-multilib gcc-multilib libboost-all-dev openssl:i386 via your package manager.
Use the provided make file via make. Requires GCC 8+ (Ambuild cannot be used).
Install vcpkg and install from manifest: vcpkg install --triplet x86-windows. This will install boost asio and openssl
Open and build using the solution file (socket_ext.sln) in VS 2022+ with the latest windows 10/11 sdk.