diff --git a/samples/streamelements-events/extension/index.ts b/samples/streamelements-events/extension/index.ts index 2975599ba..c82d2de08 100644 --- a/samples/streamelements-events/extension/index.ts +++ b/samples/streamelements-events/extension/index.ts @@ -58,6 +58,10 @@ module.exports = function (nodecg: NodeCG) { ); } }); + + client.onTest((data) => { + nodecg.log.info(JSON.stringify(data)); + }); }); streamElements?.onUnavailable(() => nodecg.log.info("SE client has been unset.")); diff --git a/services/nodecg-io-streamelements/extension/StreamElements.ts b/services/nodecg-io-streamelements/extension/StreamElements.ts index 6d526b89b..353dd91c2 100644 --- a/services/nodecg-io-streamelements/extension/StreamElements.ts +++ b/services/nodecg-io-streamelements/extension/StreamElements.ts @@ -6,7 +6,7 @@ import { EventEmitter } from "events"; export class StreamElementsServiceClient extends EventEmitter { private socket: SocketIOClient.Socket; - constructor(private jwtToken: string) { + constructor(private jwtToken: string, private handleTestEvents: boolean) { super(); } @@ -30,6 +30,13 @@ export class StreamElementsServiceClient extends EventEmitter { } this.emit(data.type, data); }); + if (this.handleTestEvents) { + this.onTestEvent((data: StreamElementsEvent) => { + if (data.listener) { + this.emit("test", data); + } + }); + } } async connect(): Promise { @@ -77,7 +84,7 @@ export class StreamElementsServiceClient extends EventEmitter { this.socket.on("connect_error", handler); } - onEvent(handler: (data: StreamElementsEvent) => void): void { + private onEvent(handler: (data: StreamElementsEvent) => void): void { this.socket.on("event", (data: StreamElementsEvent) => { if (data) { handler(data); @@ -85,31 +92,43 @@ export class StreamElementsServiceClient extends EventEmitter { }); } - onSubscriber(handler: (data: StreamElementsEvent) => void): void { + private onTestEvent(handler: (data: StreamElementsEvent) => void): void { + this.socket.on("event:test", (data: StreamElementsEvent) => { + if (data) { + handler(data); + } + }); + } + + public onSubscriber(handler: (data: StreamElementsEvent) => void): void { this.on("subscriber", handler); } - onTip(handler: (data: StreamElementsEvent) => void): void { + public onTip(handler: (data: StreamElementsEvent) => void): void { this.on("tip", handler); } - onCheer(handler: (data: StreamElementsEvent) => void): void { + public onCheer(handler: (data: StreamElementsEvent) => void): void { this.on("cheer", handler); } - onGift(handler: (data: StreamElementsEvent) => void): void { + public onGift(handler: (data: StreamElementsEvent) => void): void { this.on("gift", handler); } - onFollow(handler: (data: StreamElementsEvent) => void): void { + public onFollow(handler: (data: StreamElementsEvent) => void): void { this.on("follow", handler); } - onRaid(handler: (data: StreamElementsEvent) => void): void { + public onRaid(handler: (data: StreamElementsEvent) => void): void { this.on("raid", handler); } - onHost(handler: (data: StreamElementsEvent) => void): void { + public onHost(handler: (data: StreamElementsEvent) => void): void { this.on("host", handler); } + + public onTest(handler: (data: StreamElementsEvent) => void): void { + this.on("test", handler); + } } diff --git a/services/nodecg-io-streamelements/extension/StreamElementsEvent.ts b/services/nodecg-io-streamelements/extension/StreamElementsEvent.ts index b7be72ae5..40dcc9392 100644 --- a/services/nodecg-io-streamelements/extension/StreamElementsEvent.ts +++ b/services/nodecg-io-streamelements/extension/StreamElementsEvent.ts @@ -85,5 +85,15 @@ export interface StreamElementsEvent { * Event update date and time */ updatedAt: string; + /** + * Listener property for test events + */ + listener?: string; + /** + * Event property for test events + */ + event?: { + [k: string]: unknown; + }; [k: string]: unknown; } diff --git a/services/nodecg-io-streamelements/extension/index.ts b/services/nodecg-io-streamelements/extension/index.ts index 2466da8af..d6193b19b 100644 --- a/services/nodecg-io-streamelements/extension/index.ts +++ b/services/nodecg-io-streamelements/extension/index.ts @@ -4,6 +4,7 @@ import { StreamElementsServiceClient } from "./StreamElements"; interface StreamElementsServiceConfig { jwtToken: string; + handleTestEvents: boolean; } export { StreamElementsServiceClient } from "./StreamElements"; @@ -15,12 +16,12 @@ module.exports = (nodecg: NodeCG) => { class StreamElementsService extends ServiceBundle { async validateConfig(config: StreamElementsServiceConfig) { - return new StreamElementsServiceClient(config.jwtToken).testConnection(); + return new StreamElementsServiceClient(config.jwtToken, config.handleTestEvents).testConnection(); } async createClient(config: StreamElementsServiceConfig, logger: Logger) { logger.info("Connecting to StreamElements socket server..."); - const client = new StreamElementsServiceClient(config.jwtToken); + const client = new StreamElementsServiceClient(config.jwtToken, config.handleTestEvents); await client.connect(); logger.info("Successfully connected to StreamElements socket server."); diff --git a/services/nodecg-io-streamelements/package.json b/services/nodecg-io-streamelements/package.json index fa6e9696c..cf4d418c0 100644 --- a/services/nodecg-io-streamelements/package.json +++ b/services/nodecg-io-streamelements/package.json @@ -43,6 +43,6 @@ "dependencies": { "@types/socket.io-client": "^1.4.36", "nodecg-io-core": "^0.2.0", - "socket.io-client": "^4.3.2" + "socket.io-client": "^2.4.0" } } diff --git a/services/nodecg-io-streamelements/streamelements-schema.json b/services/nodecg-io-streamelements/streamelements-schema.json index f971af224..da56ed330 100644 --- a/services/nodecg-io-streamelements/streamelements-schema.json +++ b/services/nodecg-io-streamelements/streamelements-schema.json @@ -6,6 +6,11 @@ "jwtToken": { "type": "string", "description": "Your JWT token for streamelments." + }, + "handleTestEvents": { + "type": "boolean", + "default": false, + "description": "Whether test events should be handled." } }, "required": ["jwtToken"]