Skip to content

Commit ca03d46

Browse files
committed
First implementation of StreamlabsAPI;
1 parent a070d8f commit ca03d46

4 files changed

Lines changed: 67 additions & 65 deletions

File tree

StreamlabsAPI.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "StreamlabsAPI.h"
2+
#include <ArduinoJson.h>
3+
4+
5+
void StreamlabsAPI::loop() {
6+
webSocket.loop();
7+
}
8+
9+
bool StreamlabsAPI::connect(const char* socketToken) {
10+
webSocket.on("event", std::bind(&StreamlabsAPI::event, this, std::placeholders::_1, std::placeholders::_2));
11+
12+
String queryParams = SL_DEFAULT_QUERYPARAMETES;
13+
queryParams += "&token=";
14+
queryParams += socketToken;
15+
webSocket.beginSSL(SL_HOSTNAME, SL_PORT, queryParams.c_str(), SL_FINGERPRINT);
16+
}
17+
18+
19+
void StreamlabsAPI::followTwitchEvent(std::function<void (const char * payload)> func) {
20+
events["follow"] = func;
21+
}
22+
23+
void StreamlabsAPI::event(const char * payload, size_t length) {
24+
StaticJsonDocument<200> doc;
25+
DeserializationError error = deserializeJson(doc, payload);
26+
if (error) {
27+
Serial.print("deserializeJson() failed: ");
28+
return;
29+
}
30+
31+
const char* type = doc["type"];
32+
auto e = events.find(type);
33+
if(e != events.end()) {
34+
e->second(payload);
35+
}
36+
}

StreamlabsAPI.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <Arduino.h>
2+
#include <map>
3+
#include <SocketIoClient.h>
4+
5+
#define SL_HOSTNAME "sockets.streamlabs.com"
6+
#define SL_PORT 443
7+
#define SL_DEFAULT_QUERYPARAMETES "/socket.io/?transport=websocket"
8+
#define SL_FINGERPRINT "E7 93 77 36 DA D4 15 0F C1 C1 8F 14 D5 2A C8 72 93 D0 6F 2A"
9+
10+
11+
#ifndef STREAMLABSAPI_H_
12+
#define STREAMLABSAPI_H_
13+
14+
class StreamlabsAPI {
15+
private:
16+
SocketIoClient webSocket;
17+
std::map<String, std::function<void (const char * payload)>> events;
18+
void event(const char * payload, size_t length);
19+
public:
20+
void loop();
21+
bool connect(const char* socketToken);
22+
void followTwitchEvent(std::function<void (const char * payload)> func);
23+
void subscriptionsTwitchEvent(std::function<void (const char * payload)> func);
24+
void hostTwitchEvent(std::function<void (const char * payload)> func);
25+
void bitsTwitchEvent(std::function<void (const char * payload)> func);
26+
void raidsTwitchEvent(std::function<void (const char * payload)> func);
27+
void donationEvent(std::function<void (const char * payload)> func);
28+
};
29+
30+
31+
#endif /* STREAMLABSAPI_H_ */

StreamlabsArduinoAlerts.ino

Lines changed: 0 additions & 56 deletions
This file was deleted.

config.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)