2727/* *
2828 *
2929 * @param client WSclient_t * ptr to the client struct
30- * @param code
30+ * @param code uint16_t see RFC
3131 * @param reason
3232 * @param reasonLen
3333 */
3434void WebSockets::clientDisconnect (WSclient_t * client, uint16_t code, char * reason, size_t reasonLen) {
3535 if (client->status == WSC_CONNECTED && code) {
36- // todo send reason to client
37-
38- if (reasonLen > 0 && reason) {
39-
40- } else {
41-
42- }
36+ sendFrame (client, WSop_close, (uint8_t *) reason, reasonLen);
4337 }
4438 clientDisconnect (client);
4539}
@@ -49,9 +43,9 @@ void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * rea
4943 * @param client WSclient_t * ptr to the client struct
5044 * @param opcode WSopcode_t
5145 * @param payload uint8_t *
52- * @param lenght size_t
46+ * @param length size_t
5347 */
54- void WebSockets::sendFrame (WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t lenght ) {
48+ void WebSockets::sendFrame (WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length ) {
5549
5650 uint8_t buffer[16 ] = { 0 };
5751 uint8_t i = 0 ;
@@ -61,31 +55,32 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
6155 buffer[i] = bit (7 ); // set Fin
6256 buffer[i++] |= opcode; // set opcode
6357
64- if (lenght < 126 ) {
65- buffer[i++] = lenght ;
58+ if (length < 126 ) {
59+ buffer[i++] = length ;
6660
67- } else if (lenght < 0xFFFF ) {
61+ } else if (length < 0xFFFF ) {
6862 buffer[i++] = 126 ;
69- buffer[i++] = ((lenght >> 8 ) & 0xFF );
70- buffer[i++] = (lenght & 0xFF );
63+ buffer[i++] = ((length >> 8 ) & 0xFF );
64+ buffer[i++] = (length & 0xFF );
7165 } else {
66+ // normaly we never get here (to less memory)
7267 buffer[i++] = 127 ;
7368 buffer[i++] = 0x00 ;
7469 buffer[i++] = 0x00 ;
7570 buffer[i++] = 0x00 ;
7671 buffer[i++] = 0x00 ;
77- buffer[i++] = ((lenght >> 24 ) & 0xFF );
78- buffer[i++] = ((lenght >> 16 ) & 0xFF );
79- buffer[i++] = ((lenght >> 8 ) & 0xFF );
80- buffer[i++] = (lenght & 0xFF );
72+ buffer[i++] = ((length >> 24 ) & 0xFF );
73+ buffer[i++] = ((length >> 16 ) & 0xFF );
74+ buffer[i++] = ((length >> 8 ) & 0xFF );
75+ buffer[i++] = (length & 0xFF );
8176 }
8277
8378 // send header
8479 client->tcp .write (&buffer[0 ], i);
8580
86- if (payload && lenght > 0 ) {
81+ if (payload && length > 0 ) {
8782 // send payload
88- client->tcp .write (&payload[0 ], lenght );
83+ client->tcp .write (&payload[0 ], length );
8984 }
9085
9186}
@@ -136,7 +131,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
136131 }
137132 payloadLen = buffer[0 ] << 8 | buffer[1 ];
138133 } else if (payloadLen == 127 ) {
139- // read 64bit inteager as Lenght
134+ // read 64bit inteager as length
140135 if (!readWait (client, buffer, 8 )) {
141136 // timeout
142137 clientDisconnect (client, 1002 );
@@ -191,32 +186,24 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
191186
192187 switch (opCode) {
193188 case WSop_text:
194- DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] text: %s\n " , client->num , payload)
195- ;
196- // todo API for user to get message may callback
197-
198- // send the frame back!
199- sendFrame (client, WSop_text, payload, payloadLen);
200-
201- break ;
189+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] text: %s\n " , client->num , payload);
190+ // no break here!
202191 case WSop_binary:
203- // todo API for user to get message may callback
192+ messageRecived (client, opCode, payload, payloadLen);
204193 break ;
205194 case WSop_ping:
206- // todo send pong
195+ // send pong back
196+ sendFrame (client, WSop_pong, payload, payloadLen);
207197 break ;
208198 case WSop_pong:
209- DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] get pong from Client (%s)\n " , client->num , payload)
210- ;
199+ DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] get pong from Client (%s)\n " , client->num , payload);
211200 break ;
212- case WSop_close: {
213- uint16_t reasonCode = buffer[0 ] << 8 | buffer[1 ];
214-
215- DEBUG_WEBSOCKETS (" [WS-Server][%d][handleWebsocket] client ask for close. Code: %d (%s)\n " , client->num , reasonCode, (payload + 2 ));
216-
217- // todo send confimation to client
218- clientDisconnect (client, 1000 , (char *) (payload + 2 ), payloadLen - 2 );
219- }
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 );
206+ }
220207 break ;
221208 case WSop_continuation:
222209 // continuation is not supported
0 commit comments