42 lines
1.2 KiB
C++
42 lines
1.2 KiB
C++
|
#include <ArduinoWebsockets.h>
|
||
|
|
||
|
using namespace websockets;
|
||
|
|
||
|
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";
|
||
|
|
||
|
// Reconnection parameters
|
||
|
unsigned long lastAttemptTime = 0;
|
||
|
const unsigned long reconnectInterval = 60000; // 1 minute
|
||
|
|
||
|
void onMessageCallback(WebsocketsMessage message) {
|
||
|
Serial.println("Gotify Notification:");
|
||
|
Serial.println(message.data());
|
||
|
}
|
||
|
|
||
|
void connectToGotify() {
|
||
|
String url = String(gotify_server) + "?token=" + gotify_token;
|
||
|
gotifyClient.onMessage(onMessageCallback);
|
||
|
gotifyClient.connect(url);
|
||
|
|
||
|
if (gotifyClient.available()) {
|
||
|
Serial.println("Connected to Gotify WebSocket");
|
||
|
} else {
|
||
|
Serial.println("Failed to connect to Gotify WebSocket");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void checkGotifyWebSocket() {
|
||
|
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();
|
||
|
}
|
||
|
}
|