Skip to content

Commit

Permalink
Fixed:
Browse files Browse the repository at this point in the history
- SinricPro.isConnected() still returns true if the connection was lost
- onDisconnectCallback does not fire if the connection is lost.
  • Loading branch information
sivar2311 committed Nov 30, 2023
1 parent 443cdd2 commit 0c27c8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/SinricPro.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,9 @@ void SinricProClass::handle() {
return;
}

if (WiFi.isConnected()) {
if (!isConnected()) connect();
_websocketListener.handle();
_udpListener.handle();
}
if (!isConnected()) connect();
_websocketListener.handle();
_udpListener.handle();

handleReceiveQueue();
handleSendQueue();
Expand Down
16 changes: 14 additions & 2 deletions src/SinricProWebsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
#include "SinricProQueue.h"
namespace SINRICPRO_NAMESPACE {

enum class ConnectionState {
disconnected,
connecting,
connected
};

#if !defined(WEBSOCKETS_VERSION_INT) || (WEBSOCKETS_VERSION_INT < 2003005)
#error "Wrong WebSockets Version! Minimum Version is 2.3.5!!!"
#endif
Expand Down Expand Up @@ -53,6 +59,7 @@ class WebsocketListener : protected WebSocketsClient {
protected:
bool _begin;
bool restoreDeviceStates;
ConnectionState connectionState;

wsConnectedCallback _wsConnectedCb;
wsDisconnectedCallback _wsDisconnectedCb;
Expand All @@ -69,6 +76,7 @@ class WebsocketListener : protected WebSocketsClient {
WebsocketListener::WebsocketListener()
: _begin(false)
, restoreDeviceStates(false)
, connectionState(ConnectionState::disconnected)
, _wsConnectedCb(nullptr)
, _wsDisconnectedCb(nullptr)
, _wsPongCb(nullptr) {}
Expand Down Expand Up @@ -105,6 +113,7 @@ void WebsocketListener::setExtraHeaders() {
void WebsocketListener::begin(String server, String appKey, String deviceIds, SinricProQueue_t* receiveQueue) {
if (_begin) return;
_begin = true;
connectionState = ConnectionState::connecting;

this->receiveQueue = receiveQueue;
this->appKey = appKey;
Expand Down Expand Up @@ -133,6 +142,7 @@ void WebsocketListener::handle() {
void WebsocketListener::stop() {
disconnect();
_begin = false;
connectionState = ConnectionState::disconnected;
}

void WebsocketListener::setRestoreDeviceStates(bool flag) {
Expand Down Expand Up @@ -161,8 +171,9 @@ void WebsocketListener::runCbEvent(WStype_t type, uint8_t* payload, size_t lengt
switch (type) {
case WStype_DISCONNECTED: {
DEBUG_SINRIC("[SinricPro:Websocket]: disconnected\r\n");
if (_wsDisconnectedCb) _wsDisconnectedCb();
}
if (connectionState == ConnectionState::connected && _wsDisconnectedCb) _wsDisconnectedCb();
connectionState = ConnectionState::disconnected;
}
break;

case WStype_CONNECTED:
Expand All @@ -172,6 +183,7 @@ void WebsocketListener::runCbEvent(WStype_t type, uint8_t* payload, size_t lengt
restoreDeviceStates = false;
setExtraHeaders();
}
connectionState = ConnectionState::connected;
break;

case WStype_TEXT: {
Expand Down

0 comments on commit 0c27c8c

Please sign in to comment.