2024-06-06 20:50:21 +00:00
|
|
|
#include "Gotify.h"
|
2024-06-06 00:04:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
WebsocketsClient gotifyClient;
|
|
|
|
|
|
|
|
const char* gotify_server = "ws://your.gotify.server.com/stream"; // Use ws:// or wss:// based on your server configuration
|
|
|
|
const char* gotify_token = "your_gotify_app_token";
|
|
|
|
|
|
|
|
unsigned long lastAttemptTime = 0;
|
|
|
|
const unsigned long reconnectInterval = 60000; // 1 minute
|
|
|
|
|
2024-06-06 20:50:21 +00:00
|
|
|
|
2024-06-06 00:04:40 +00:00
|
|
|
void onMessageCallback(WebsocketsMessage message) {
|
|
|
|
Serial.println("Gotify Notification:");
|
|
|
|
Serial.println(message.data());
|
2024-06-06 20:50:21 +00:00
|
|
|
playNotificationSound();
|
2024-06-06 00:04:40 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:50:21 +00:00
|
|
|
|
2024-06-06 00:04:40 +00:00
|
|
|
void connectToGotify() {
|
|
|
|
String url = String(gotify_server) + "?token=" + gotify_token;
|
|
|
|
gotifyClient.onMessage(onMessageCallback);
|
|
|
|
gotifyClient.connect(url);
|
|
|
|
|
2024-06-06 20:50:21 +00:00
|
|
|
if (gotifyClient.available())
|
2024-06-06 00:04:40 +00:00
|
|
|
Serial.println("Connected to Gotify WebSocket");
|
2024-06-06 20:50:21 +00:00
|
|
|
else
|
2024-06-06 00:04:40 +00:00
|
|
|
Serial.println("Failed to connect to Gotify WebSocket");
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:50:21 +00:00
|
|
|
|
|
|
|
void loopGotifyWebSocket() {
|
|
|
|
while (true) {
|
|
|
|
if (!gotifyClient.available()) {
|
|
|
|
unsigned long currentTime = millis();
|
|
|
|
if (currentTime - lastAttemptTime > reconnectInterval) {
|
|
|
|
Serial.println("Attempting to reconnect to Gotify WebSocket...");
|
|
|
|
lastAttemptTime = currentTime;
|
|
|
|
connectToGotify();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gotifyClient.poll();
|
2024-06-06 00:04:40 +00:00
|
|
|
}
|
2024-06-06 20:50:21 +00:00
|
|
|
|
|
|
|
delay(10);
|
2024-06-06 00:04:40 +00:00
|
|
|
}
|
2024-06-06 20:50:21 +00:00
|
|
|
}
|