This commit is contained in:
Links
2018-05-10 18:55:23 +02:00
parent 8a187b523b
commit ea8e81c6ce
2 changed files with 24 additions and 11 deletions

View File

@ -29,6 +29,7 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
_port = port;
_origin = origin;
_protocol = protocol;
_runnning = false;
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
@ -50,15 +51,7 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
WebSocketsServer::~WebSocketsServer() {
// disconnect all clients
disconnect();
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
_server->close();
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
_server->end();
#else
// TODO how to close server?
#endif
close();
if (_mandatoryHttpHeaders)
delete[] _mandatoryHttpHeaders;
@ -110,19 +103,36 @@ void WebSocketsServer::begin(void) {
randomSeed(millis());
#endif
_runnning = true;
_server->begin();
DEBUG_WEBSOCKETS("[WS-Server] Server Started.\n");
}
void WebSocketsServer::close(void) {
_runnning = false;
disconnect();
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
_server->close();
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
_server->end();
#else
// TODO how to close server?
#endif
}
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
/**
* called in arduino loop
*/
void WebSocketsServer::loop(void) {
if(_runnning) {
handleNewClients();
handleClientData();
}
}
#endif
/**

View File

@ -49,6 +49,7 @@ public:
virtual ~WebSocketsServer(void);
void begin(void);
void close(void);
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
void loop(void);
@ -113,6 +114,8 @@ protected:
WebSocketServerEvent _cbEvent;
WebSocketServerHttpHeaderValFunc _httpHeaderValidationFunc;
bool _runnning;
bool newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);