33
33
*/
34
34
void WebSockets::clientDisconnect (WSclient_t * client, uint16_t code, char * reason, size_t reasonLen) {
35
35
if (client->status == WSC_CONNECTED && code) {
36
- sendFrame (client, WSop_close, (uint8_t *) reason, reasonLen);
36
+ if (reason) {
37
+ sendFrame (client, WSop_close, (uint8_t *) reason, reasonLen);
38
+ } else {
39
+ uint8_t buffer[2 ];
40
+ buffer[0 ] = ((code >> 8 ) & 0xFF );
41
+ buffer[1 ] = (code & 0xFF );
42
+ sendFrame (client, WSop_close, &buffer[0 ], 2 );
43
+ }
37
44
}
38
45
clientDisconnect (client);
39
46
}
@@ -171,6 +178,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
171
178
172
179
if (!readWait (client, payload, payloadLen)) {
173
180
DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] missing data!\n " , client->num );
181
+ free (payload);
174
182
clientDisconnect (client, 1002 );
175
183
return ;
176
184
}
@@ -183,36 +191,51 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
183
191
payload[i] = (payload[i] ^ maskKey[i % 4 ]);
184
192
}
185
193
}
194
+ }
186
195
187
- switch (opCode) {
188
- case WSop_text:
189
- DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] text: %s\n " , client->num , payload);
190
- // no break here!
191
- case WSop_binary:
192
- messageRecived (client, opCode, payload, payloadLen);
193
- break ;
194
- case WSop_ping:
195
- // send pong back
196
- sendFrame (client, WSop_pong, payload, payloadLen);
197
- break ;
198
- case WSop_pong:
199
- DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] get pong from Client (%s)\n " , client->num , payload);
200
- break ;
201
- case WSop_close:
202
- {
203
- uint16_t reasonCode = buffer[0 ] << 8 | buffer[1 ];
204
- DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] client ask for close. Code: %d (%s)\n " , client->num , reasonCode, (payload + 2 ));
205
- clientDisconnect (client, 1000 );
196
+ switch (opCode) {
197
+ case WSop_text:
198
+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] text: %s\n " , client->num , payload)
199
+ ;
200
+ // no break here!
201
+ case WSop_binary:
202
+ messageRecived (client, opCode, payload, payloadLen);
203
+ break ;
204
+ case WSop_ping:
205
+ // send pong back
206
+ sendFrame (client, WSop_pong, payload, payloadLen);
207
+ break ;
208
+ case WSop_pong:
209
+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] get pong from Client (%s)\n " , client->num , payload)
210
+ ;
211
+ break ;
212
+ case WSop_close:
213
+ {
214
+ uint16_t reasonCode = 1000 ;
215
+ if (payloadLen >= 2 ) {
216
+ reasonCode = payload[0 ] << 8 | payload[1 ];
206
217
}
207
- break ;
208
- case WSop_continuation:
209
- // continuation is not supported
210
- clientDisconnect (client, 1003 );
211
- break ;
212
- default :
213
- clientDisconnect (client, 1002 );
214
- break ;
215
- }
218
+
219
+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] client ask for close. Code: %d" , client->num , reasonCode);
220
+ if (payloadLen > 2 ) {
221
+ DEBUG_WEBSOCKETS (" (%s)\n " , (payload+2 ));
222
+ } else {
223
+ DEBUG_WEBSOCKETS (" \n " );
224
+ }
225
+ clientDisconnect (client, 1000 );
226
+ }
227
+ break ;
228
+ case WSop_continuation:
229
+ // continuation is not supported
230
+ clientDisconnect (client, 1003 );
231
+ break ;
232
+ default :
233
+ clientDisconnect (client, 1002 );
234
+ break ;
235
+ }
236
+
237
+ if (payload) {
238
+ free (payload);
216
239
}
217
240
218
241
}
0 commit comments