mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-16 16:52:05 +02:00
add close #322
This commit is contained in:
@ -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) {
|
||||||
handleNewClients();
|
if(_runnning) {
|
||||||
handleClientData();
|
handleNewClients();
|
||||||
|
handleClientData();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user