Websocket connection
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include "SwimTrackerConfig.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
|
||||
// Own libs
|
||||
#include "MockScale.h"
|
||||
@@ -14,11 +16,13 @@
|
||||
#include "SimpleMeasurementSession.h"
|
||||
#include "EspHttp.h"
|
||||
#include "WebDAV.h"
|
||||
#include "WebsocketServer.h"
|
||||
|
||||
using Session_T = SimpleMeasurementSession<MeasurementT, CONFIG_SESSION_MAX_SIZE>;
|
||||
SessionManager<Session_T> sessionManager(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN, CONFIG_TARE_AVG_COUNT);
|
||||
|
||||
EspHttp espHttpServer;
|
||||
WebsocketServer<Session_T> webSocketServer(sessionManager, 81);
|
||||
|
||||
template <typename SessionT>
|
||||
void httpSetup(SessionManager<SessionT> *sessionManager)
|
||||
@@ -175,9 +179,43 @@ void setup()
|
||||
|
||||
// HTTP & Websocket server
|
||||
httpSetup(&sessionManager);
|
||||
|
||||
webSocketServer.begin();
|
||||
}
|
||||
|
||||
int measurementsSent = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
sessionManager.iteration();
|
||||
webSocketServer.iteration();
|
||||
/*
|
||||
if (webSocketServer.poll())
|
||||
{
|
||||
websocketClients[nextFreeWebsocketClient] = webSocketServer.accept();
|
||||
websocketClients[nextFreeWebsocketClient].onMessage(onMessage);
|
||||
Serial.println("Websocket connection");
|
||||
nextFreeWebsocketClient = (nextFreeWebsocketClient + 1) % MAX_WEBSOCKET_CONNECTIONS;
|
||||
}
|
||||
for (int i = 0; i < MAX_WEBSOCKET_CONNECTIONS; ++i)
|
||||
//if (websocketClients[i].available()) {
|
||||
//Serial.printf("Polling client %d\n", i);
|
||||
websocketClients[i].poll();
|
||||
//}
|
||||
|
||||
auto &session = sessionManager.session();
|
||||
if (session.numMeasurements() < measurementsSent)
|
||||
measurementsSent = 0;
|
||||
else if (session.numMeasurements() > measurementsSent)
|
||||
{
|
||||
for (int i = 0; i < MAX_WEBSOCKET_CONNECTIONS; ++i)
|
||||
if (websocketClients[i].available())
|
||||
{
|
||||
auto dataToSend = (const char*)(session.getDataPointer() + measurementsSent);
|
||||
auto numBytes = (session.numMeasurements() - measurementsSent) * sizeof(MeasurementT);
|
||||
Serial.printf("Sent %d bytes via websocket\n", numBytes);
|
||||
websocketClients[i].sendBinary(dataToSend, numBytes);
|
||||
measurementsSent = session.numMeasurements();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user