Skip to content

Commit 21e402f

Browse files
authored
Merge pull request #9 from lucalas/feature/2-replace-socket-io-client-library
Replaced socket io client library
2 parents c9e7498 + c9c2ee4 commit 21e402f

File tree

5 files changed

+73
-35
lines changed

5 files changed

+73
-35
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

22
# Streamlabs Arduino Alerts [![Build Status](https://travis-ci.com/lucalas/StreamlabsArduinoAlerts.svg?branch=master)](https://travis-ci.com/lucalas/StreamlabsArduinoAlerts)
3-
A work in progress library to connect to your Streamlabs account and catch all the Twitch events.
3+
Library to connect to your Streamlabs account and catch all the `Twitch` events.
44

5-
## Twitch event catchable
5+
## Twitch catchable events
66

77
- Follow
88
- Subscription
9+
- Resubscription
910
- Donation
1011
- Raid
1112
- Host
@@ -24,18 +25,18 @@ A work in progress library to connect to your Streamlabs account and catch all t
2425
## Add library
2526
Best thing is to use the Arduino Library Manager.
2627

27-
Go to Sketch > Include Library > Manage Libraries.
28-
Install WebSockets by Markus Sattler
29-
Install SocketIoClient
30-
Install StreamlabsArduinoAlerts
31-
Select Sketch > Include Library > StreamlabsArduinoAlerts
28+
- Go to Sketch > Include Library > Manage Libraries.
29+
- Install `WebSockets` by Markus Sattler
30+
- Install `StreamlabsArduinoAlerts`
31+
- Select Sketch > Include Library > StreamlabsArduinoAlerts
3232

3333
## Methods
3434
#### connect(socket_token)
35-
Method to connect to Streamlabs server, required [socket token](https://streamlabs.readme.io/docs/sockettoken).
35+
Method to connect to Streamlabs server, required [socket token](#get-streamlabs-socket-token).
3636

3737
#### followTwitchEvent(callback)
3838
#### subscriptionsTwitchEvent(callback)
39+
#### resubscriptionsTwitchEvent(callback)
3940
#### hostTwitchEvent(callback)
4041
#### bitsTwitchEvent(callback)
4142
#### raidsTwitchEvent(callback)
@@ -68,5 +69,8 @@ void loop() {
6869

6970
Event result documentation can be found on Streamlabs WebSite [HERE](https://dev.streamlabs.com/docs/socket-api)
7071

72+
## Get Streamlabs Socket Token
73+
You can find yours at https://streamlabs.com/dashboard#/apisettings under "API TOKENS" then "Your Socket API Token". This token is bound to the account that you are logged in with. If you need to access the alerts of a streamer, ask them to provide you with a token from the above location.
74+
7175
### Issues ###
7276
Submit issues to: https://github.com/lucalas/StreamlabsArduinoAlerts/issues

examples/esp8266/StreamlabsArduinoAlerts/StreamlabsArduinoAlerts.ino

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,26 @@ void subscriptionEvent(const char * payload) {
2727
Serial.println(payload);
2828
}
2929

30+
void resubscriptionEvent(const char * payload) {
31+
Serial.print("got resubscriptionEvent message: ");
32+
Serial.println(payload);
33+
}
34+
35+
void hostEvent(const char * payload) {
36+
Serial.print("got hostEvent message: ");
37+
Serial.println(payload);
38+
}
39+
3040
void bitsEvent(const char * payload) {
3141
Serial.print("got bitsEvent message: ");
3242
Serial.println(payload);
3343
}
3444

45+
void raidEvent(const char * payload) {
46+
Serial.print("got raidEvent message: ");
47+
Serial.println(payload);
48+
}
49+
3550
void donationsEvent(const char * payload) {
3651
Serial.print("got donationsEvent message: ");
3752
Serial.println(payload);
@@ -57,7 +72,10 @@ void setup() {
5772

5873
streamlabsAPI.followTwitchEvent(followerEvent);
5974
streamlabsAPI.subscriptionsTwitchEvent(subscriptionEvent);
75+
streamlabsAPI.resubscriptionsTwitchEvent(resubscriptionEvent);
76+
streamlabsAPI.hostTwitchEvent(hostEvent);
6077
streamlabsAPI.bitsTwitchEvent(bitsEvent);
78+
streamlabsAPI.raidTwitchEvent(raidEvent);
6179
streamlabsAPI.donationEvent(donationsEvent);
6280
streamlabsAPI.connect(SOCKET_TOKEN);
6381
}

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=StreamlabsArduinoAlerts
2-
version=0.1
2+
version=1.0
33
author=Luca Taddeo
44
maintainer=Luca Taddeo
55
sentence=Library to get Streamlabs event from Twitch
6-
paragraph=A work in progress library to connect to your Streamlabs account and catch all the Twitch events.
6+
paragraph=Library to connect to your Streamlabs account and catch all the Twitch events.
77
category=Communication
88
url=https://github.com/lucalas/StreamlabsArduinoAlerts
99
architectures=*
10-
depends=SocketIoClient, WebSockets
10+
depends=WebSockets

src/StreamlabsAPI.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
#include "StreamlabsAPI.h"
2-
#include <ArduinoJson.h>
32

43

54
void StreamlabsAPI::loop() {
65
webSocket.loop();
76
}
87

9-
bool StreamlabsAPI::connect(const char* socketToken) {
10-
webSocket.on("event", std::bind(&StreamlabsAPI::event, this, std::placeholders::_1, std::placeholders::_2));
11-
8+
void StreamlabsAPI::connect(const char* socketToken) {
9+
webSocket.onEvent(std::bind(&StreamlabsAPI::event, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
1210
String queryParams = SL_DEFAULT_QUERYPARAMETES;
13-
queryParams += "&token=";
14-
queryParams += socketToken;
15-
webSocket.beginSSL(SL_HOSTNAME, SL_PORT, queryParams.c_str(), SL_FINGERPRINT);
11+
queryParams.concat("token=");
12+
queryParams.concat(socketToken);
13+
webSocket.beginSSL(SL_HOSTNAME, SL_PORT, queryParams.c_str());
1614
}
1715

1816
void StreamlabsAPI::followTwitchEvent(std::function<void (const char * payload)> func) {
@@ -23,6 +21,10 @@ void StreamlabsAPI::subscriptionsTwitchEvent(std::function<void (const char * pa
2321
events["subscription"] = func;
2422
}
2523

24+
void StreamlabsAPI::resubscriptionsTwitchEvent(std::function<void (const char * payload)> func) {
25+
events["resub"] = func;
26+
}
27+
2628
void StreamlabsAPI::hostTwitchEvent(std::function<void (const char * payload)> func) {
2729
events["host"] = func;
2830
}
@@ -39,18 +41,22 @@ void StreamlabsAPI::donationEvent(std::function<void (const char * payload)> fun
3941
events["donation"] = func;
4042
}
4143

42-
StaticJsonDocument<10000> doc;
43-
void StreamlabsAPI::event(const char * payload, size_t length) {
44-
// FIXME remove deserialized and parse string to get the type
45-
DeserializationError error = deserializeJson(doc, payload);
46-
if (error) {
47-
Serial.print("deserializeJson() failed: ");
48-
return;
49-
}
44+
void StreamlabsAPI::event(socketIOmessageType_t type, uint8_t * payload, size_t length) {
45+
const char* data = reinterpret_cast<char*>(payload);
46+
DEBUG_STREAMLABS_ALERTS("------STREAMLABS EVENT------\n");
47+
DEBUG_STREAMLABS_ALERTS("SocketIO message type: %d\n", type);
48+
DEBUG_STREAMLABS_ALERTS("SocketIO data: %s\n", data);
49+
50+
String payloadS = String((char *) payload);
51+
int startOfType = payloadS.indexOf("\"type\":\"");
52+
int endOfType = payloadS.indexOf("\"", startOfType + 8);
53+
String typeS = payloadS.substring(startOfType + 8, endOfType);
54+
55+
DEBUG_STREAMLABS_ALERTS("Streamlabs event type: %s\n", typeS.c_str());
56+
DEBUG_STREAMLABS_ALERTS("-----------------------------\n");
5057

51-
const char* type = doc["type"];
52-
auto e = events.find(type);
58+
auto e = events.find(typeS);
5359
if(e != events.end()) {
54-
e->second(payload);
60+
e->second(data);
5561
}
5662
}

src/StreamlabsAPI.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
#include <Arduino.h>
22
#include <map>
3-
#include <SocketIoClient.h>
3+
#include <SocketIOclient.h>
4+
5+
#ifdef DEBUG_ESP_PORT
6+
#define DEBUG_STREAMLABS_ALERTS(...) \
7+
{ \
8+
DEBUG_ESP_PORT.printf(__VA_ARGS__); \
9+
DEBUG_ESP_PORT.flush(); \
10+
}
11+
#else
12+
#define DEBUG_STREAMLABS_ALERTS(...)
13+
#endif
414

515
#define SL_HOSTNAME "sockets.streamlabs.com"
616
#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"
17+
#define SL_DEFAULT_QUERYPARAMETES "/socket.io/?"
918

1019

1120
#ifndef STREAMLABSAPI_H_
1221
#define STREAMLABSAPI_H_
1322

1423
class StreamlabsAPI {
1524
private:
16-
SocketIoClient webSocket;
25+
SocketIOclient webSocket;
1726
std::map<String, std::function<void (const char * payload)>> events;
18-
void event(const char * payload, size_t length);
27+
void event(socketIOmessageType_t type, uint8_t * payload, size_t length);
1928
public:
2029
void loop();
21-
bool connect(const char* socketToken);
30+
void connect(const char* socketToken);
2231
void followTwitchEvent(std::function<void (const char * payload)> func);
2332
void subscriptionsTwitchEvent(std::function<void (const char * payload)> func);
33+
void resubscriptionsTwitchEvent(std::function<void (const char * payload)> func);
2434
void hostTwitchEvent(std::function<void (const char * payload)> func);
2535
void bitsTwitchEvent(std::function<void (const char * payload)> func);
2636
void raidsTwitchEvent(std::function<void (const char * payload)> func);

0 commit comments

Comments
 (0)