An event emitter that will emit a message event when a valid UDP log is created on the server
When a log is sent, there will be a message
event published that you can extract the data from. This is a valid payload. It is important to check the IP or password of the content to ensure it came from a trusted source
You can listen on socket events such as error
and close
by listening to those events. All default events of the Socket will be forwarded. If you need to close the socket at any time, provide an AbortSignal. Aborting will automatically call Socket.close for you on the socket and emits a close
event
Simple receiver that logs to console every message
Simple receiver that logs to console every message
import { LogReceiver} from "@c43721/srcds-log-receiver"; const receiver = new LogReceiver({ address: "0.0.0.0", port: 9871, }); receiver.on("event", (message) => console.log(message));
On every log generated by the srcds server, it will log that message out to console
Passing in an abort controller to close the socket
Passing in an abort controller to close the socket
import { LogReceiver} from "@c43721/srcds-log-receiver"; const controller = new AbortController(); const { signal } = controller; const receiver = new LogReceiver({ address: "0.0.0.0", port: 9871, }); receiver.on("event", (message) => console.log(message)); receiver.on("close", () => console.log("Closed the socket")); controller.abort();
While not required, there may be times where you have short-lived receivers. Since each instance creates a socket, closing those sockets becomes a problem. Abort controllers help with that by providing a way to explicitly close the resources for you
Using the password and IP to verify the source of the data
Using the password and IP to verify the source of the data
import { LogReceiver} from "@c43721/srcds-log-receiver"; const receiver = new LogReceiver({ address: "0.0.0.0", port: 9871, }); receiver.on("event", (message) => { if (message.password === null) { return; } if (message.password === "mysuperdupersecretlogpassword") { await doSomethingWithTheMessage(message); } });
For security reasons, you should always use a log secret to prevent evaluation of potentially malicious messages. Do this by looking at the password field. In order to set up the log secret, you can use the sv_logsecret
command
LogReceiver(options?: LogReceiverOptions)
Creates a new receiver