Fix socket bugs, timeout for connection
This commit is contained in:
parent
4c23565b9c
commit
6f8838c32b
@ -84,6 +84,8 @@ private:
|
||||
void disconnect();
|
||||
|
||||
bool shouldReconnect = true;
|
||||
bool isConnecting = false;
|
||||
uint32_t connectionTimeout = 0;
|
||||
uint32_t nextReconnectAttemptMs = 0;
|
||||
|
||||
void didDisconnect();
|
||||
|
@ -22,6 +22,11 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
|
||||
|
||||
Ethernet.init(ethernetConfig.spiPinSS);
|
||||
|
||||
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
|
||||
Serial.print("[INFO] DHCP assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
ethernetIsConfigured = true;
|
||||
} else {
|
||||
// Check for Ethernet hardware present
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("[ERROR] Ethernet shield not found.");
|
||||
@ -31,11 +36,7 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
|
||||
Serial.println("[ERROR] Ethernet cable status unknown.");
|
||||
} else if (Ethernet.linkStatus() == LinkON) {
|
||||
Serial.println("[INFO] Ethernet cable is connected.");
|
||||
if (Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.dhcpLeaseTimeoutMs, ethernetConfig.dhcpLeaseResponseTimeoutMs) == 1) {
|
||||
Serial.print("[INFO] DHCP assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
ethernetIsConfigured = true;
|
||||
} else {
|
||||
|
||||
// Try to configure using IP address instead of DHCP
|
||||
Ethernet.begin(ethernetConfig.macAddress, ethernetConfig.manualIp, ethernetConfig.manualDnsAddress);
|
||||
Serial.print("[WARNING] DHCP failed, using self-assigned IP ");
|
||||
|
@ -20,6 +20,9 @@ void ServerConnection::connect() {
|
||||
return;
|
||||
}
|
||||
|
||||
isConnecting = true;
|
||||
Serial.printf("[INFO] Connecting to %s:%d%s\n", configuration.url, configuration.port, configuration.path);
|
||||
connectionTimeout = currentTime + configuration.socketHeartbeatIntervalMs;
|
||||
webSocket.begin(configuration.url, configuration.port, configuration.path);
|
||||
webSocket.setAuthorization(configuration.key);
|
||||
|
||||
@ -31,7 +34,7 @@ void ServerConnection::connect() {
|
||||
}
|
||||
|
||||
void ServerConnection::didDisconnect() {
|
||||
if (shouldReconnect) {
|
||||
if (shouldReconnect || isConnecting) {
|
||||
return; // Disconnect already registered.
|
||||
}
|
||||
Serial.println("[INFO] Socket disconnected");
|
||||
@ -40,6 +43,7 @@ void ServerConnection::didDisconnect() {
|
||||
}
|
||||
|
||||
void ServerConnection::didConnect() {
|
||||
isConnecting = false;
|
||||
Serial.println("[INFO] Socket connected");
|
||||
webSocket.sendTXT(configuration.key);
|
||||
webSocket.enableHeartbeat(configuration.socketHeartbeatIntervalMs, configuration.socketHeartbeatTimeoutMs, configuration.socketHeartbeatFailureReconnectCount);
|
||||
@ -52,10 +56,16 @@ void ServerConnection::disconnect() {
|
||||
void ServerConnection::loop(uint32_t millis) {
|
||||
currentTime = millis;
|
||||
webSocket.loop();
|
||||
if (shouldReconnect) {
|
||||
if (shouldReconnect && !isConnecting) {
|
||||
shouldReconnect = false;
|
||||
connect();
|
||||
}
|
||||
if (isConnecting && millis > connectionTimeout) {
|
||||
Serial.println("[INFO] Failed to connect");
|
||||
disconnect();
|
||||
shouldReconnect = true;
|
||||
isConnecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) {
|
||||
@ -75,11 +85,13 @@ switch(type) {
|
||||
case WStype_PONG:
|
||||
break;
|
||||
case WStype_PING:
|
||||
break;
|
||||
case WStype_ERROR:
|
||||
case WStype_FRAGMENT_TEXT_START:
|
||||
case WStype_FRAGMENT_BIN_START:
|
||||
case WStype_FRAGMENT:
|
||||
case WStype_FRAGMENT_FIN:
|
||||
Serial.printf("[WARN] Unexpected socket event %d\n", type);
|
||||
controller->sendServerError(MessageResult::UnexpectedSocketEvent);
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user