8000 add close #322 · blackhack/arduinoWebSockets@ea8e81c · GitHub
[go: up one dir, main page]

Skip to content

Commit ea8e81c

Browse files
committed
add close Links2004#322
1 parent 8a187b5 commit ea8e81c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/WebSocketsServer.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
2929
_port = port;
3030
_origin = origin;
3131
_protocol = protocol;
32+
_runnning = false;
3233

3334
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
3435

@@ -50,15 +51,7 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
5051

5152
WebSocketsServer::~WebSocketsServer() {
5253
// disconnect all clients
53-
disconnect();
54-
55-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
56-
_server->close();
57-
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
58-
_server->end();
59-
#else
60-
// TODO how to close server?
61-
#endif
54+
close();
6255

6356
if (_mandatoryHttpHeaders)
6457
delete[] _mandatoryHttpHeaders;
@@ -110,18 +103,35 @@ void WebSocketsServer::begin(void) {
110103
randomSeed(millis());
111104
#endif
112105

106+
_runnning = true;
113107
_server->begin();
114108

115109
DEBUG_WEBSOCKETS("[WS-Server] Server Started.\n");
116110
}
117111

112+
void WebSocketsServer::close(void) {
113+
_runnning = false;
114+
disconnect();
115+
116+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
117+
_server->close();
118+
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
119+
_server->end();
120+
#else
121+
// TODO how to close server?
122+
#endif
123+
124+
}
125+
118126
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
119127
/**
120128
* called in arduino loop
121129
*/
122130
void WebSocketsServer::loop(void) {
123-
handleNewClients();
124-
handleClientData();
131+
if(_runnning) {
132+
handleNewClients();
133+
handleClientData();
134+
}
125135
}
126136
#endif
127137

src/WebSocketsServer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class WebSocketsServer: protected WebSockets {
4949
virtual ~WebSocketsServer(void);
5050

5151
void begin(void);
52+
void close(void);
5253

5354
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
5455
void loop(void);
@@ -113,6 +114,8 @@ class WebSocketsServer: protected WebSockets {
113114
WebSocketServerEvent _cbEvent;
114115
WebSocketServerHttpHeaderValFunc _httpHeaderValidationFunc;
115116

117+
bool _runnning;
118+
116119
bool newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
117120

118121
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);

0 commit comments

Comments
 (0)
0