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

View File

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