Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit a55ef03

Browse files
authored
Feature: Update nodecg-io-streamelements to include test events (#287)
1 parent 2bd1414 commit a55ef03

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

samples/streamelements-events/extension/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ module.exports = function (nodecg: NodeCG) {
5858
);
5959
}
6060
});
61+
62+
client.onTest((data) => {
63+
nodecg.log.info(JSON.stringify(data));
64+
});
6165
});
6266

6367
streamElements?.onUnavailable(() => nodecg.log.info("SE client has been unset."));

services/nodecg-io-streamelements/extension/StreamElements.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EventEmitter } from "events";
66
export class StreamElementsServiceClient extends EventEmitter {
77
private socket: SocketIOClient.Socket;
88

9-
constructor(private jwtToken: string) {
9+
constructor(private jwtToken: string, private handleTestEvents: boolean) {
1010
super();
1111
}
1212

@@ -30,6 +30,13 @@ export class StreamElementsServiceClient extends EventEmitter {
3030
}
3131
this.emit(data.type, data);
3232
});
33+
if (this.handleTestEvents) {
34+
this.onTestEvent((data: StreamElementsEvent) => {
35+
if (data.listener) {
36+
this.emit("test", data);
37+
}
38+
});
39+
}
3340
}
3441

3542
async connect(): Promise<void> {
@@ -77,39 +84,51 @@ export class StreamElementsServiceClient extends EventEmitter {
7784
this.socket.on("connect_error", handler);
7885
}
7986

80-
onEvent(handler: (data: StreamElementsEvent) => void): void {
87+
private onEvent(handler: (data: StreamElementsEvent) => void): void {
8188
this.socket.on("event", (data: StreamElementsEvent) => {
8289
if (data) {
8390
handler(data);
8491
}
8592
});
8693
}
8794

88-
onSubscriber(handler: (data: StreamElementsEvent) => void): void {
95+
private onTestEvent(handler: (data: StreamElementsEvent) => void): void {
96+
this.socket.on("event:test", (data: StreamElementsEvent) => {
97+
if (data) {
98+
handler(data);
99+
}
100+
});
101+
}
102+
103+
public onSubscriber(handler: (data: StreamElementsEvent) => void): void {
89104
this.on("subscriber", handler);
90105
}
91106

92-
onTip(handler: (data: StreamElementsEvent) => void): void {
107+
public onTip(handler: (data: StreamElementsEvent) => void): void {
93108
this.on("tip", handler);
94109
}
95110

96-
onCheer(handler: (data: StreamElementsEvent) => void): void {
111+
public onCheer(handler: (data: StreamElementsEvent) => void): void {
97112
this.on("cheer", handler);
98113
}
99114

100-
onGift(handler: (data: StreamElementsEvent) => void): void {
115+
public onGift(handler: (data: StreamElementsEvent) => void): void {
101116
this.on("gift", handler);
102117
}
103118

104-
onFollow(handler: (data: StreamElementsEvent) => void): void {
119+
public onFollow(handler: (data: StreamElementsEvent) => void): void {
105120
this.on("follow", handler);
106121
}
107122

108-
onRaid(handler: (data: StreamElementsEvent) => void): void {
123+
public onRaid(handler: (data: StreamElementsEvent) => void): void {
109124
this.on("raid", handler);
110125
}
111126

112-
onHost(handler: (data: StreamElementsEvent) => void): void {
127+
public onHost(handler: (data: StreamElementsEvent) => void): void {
113128
this.on("host", handler);
114129
}
130+
131+
public onTest(handler: (data: StreamElementsEvent) => void): void {
132+
this.on("test", handler);
133+
}
115134
}

services/nodecg-io-streamelements/extension/StreamElementsEvent.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,15 @@ export interface StreamElementsEvent {
8585
* Event update date and time
8686
*/
8787
updatedAt: string;
88+
/**
89+
* Listener property for test events
90+
*/
91+
listener?: string;
92+
/**
93+
* Event property for test events
94+
*/
95+
event?: {
96+
[k: string]: unknown;
97+
};
8898
[k: string]: unknown;
8999
}

services/nodecg-io-streamelements/extension/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StreamElementsServiceClient } from "./StreamElements";
44

55
interface StreamElementsServiceConfig {
66
jwtToken: string;
7+
handleTestEvents: boolean;
78
}
89

910
export { StreamElementsServiceClient } from "./StreamElements";
@@ -15,12 +16,12 @@ module.exports = (nodecg: NodeCG) => {
1516

1617
class StreamElementsService extends ServiceBundle<StreamElementsServiceConfig, StreamElementsServiceClient> {
1718
async validateConfig(config: StreamElementsServiceConfig) {
18-
return new StreamElementsServiceClient(config.jwtToken).testConnection();
19+
return new StreamElementsServiceClient(config.jwtToken, config.handleTestEvents).testConnection();
1920
}
2021

2122
async createClient(config: StreamElementsServiceConfig, logger: Logger) {
2223
logger.info("Connecting to StreamElements socket server...");
23-
const client = new StreamElementsServiceClient(config.jwtToken);
24+
const client = new StreamElementsServiceClient(config.jwtToken, config.handleTestEvents);
2425
await client.connect();
2526
logger.info("Successfully connected to StreamElements socket server.");
2627

services/nodecg-io-streamelements/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343
"dependencies": {
4444
"@types/socket.io-client": "^1.4.36",
4545
"nodecg-io-core": "^0.2.0",
46-
"socket.io-client": "^4.3.2"
46+
"socket.io-client": "^2.4.0"
4747
}
4848
}

services/nodecg-io-streamelements/streamelements-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
"jwtToken": {
77
"type": "string",
88
"description": "Your JWT token for streamelments."
9+
},
10+
"handleTestEvents": {
11+
"type": "boolean",
12+
"default": false,
13+
"description": "Whether test events should be handled."
914
}
1015
},
1116
"required": ["jwtToken"]

0 commit comments

Comments
 (0)