27
27
/* *
28
28
*
29
29
* @param client WSclient_t * ptr to the client struct
30
- * @param code
30
+ * @param code uint16_t see RFC
31
31
* @param reason
32
32
* @param reasonLen
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
- // 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);
43
37
}
44
38
clientDisconnect (client);
45
39
}
@@ -49,9 +43,9 @@ void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * rea
49
43
* @param client WSclient_t * ptr to the client struct
50
44
* @param opcode WSopcode_t
51
45
* @param payload uint8_t *
52
- * @param lenght size_t
46
+ * @param length size_t
53
47
*/
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 ) {
55
49
56
50
uint8_t buffer[16 ] = { 0 };
57
51
uint8_t i = 0 ;
@@ -61,31 +55,32 @@ void WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
61
55
buffer[i] = bit (7 ); // set Fin
62
56
buffer[i++] |= opcode; // set opcode
63
57
64
- if (lenght < 126 ) {
65
- buffer[i++] = lenght ;
58
+ if (length < 126 ) {
59
+ buffer[i++] = length ;
66
60
67
- } else if (lenght < 0xFFFF ) {
61
+ } else if (length < 0xFFFF ) {
68
62
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 );
71
65
} else {
66
+ // normaly we never get here (to less memory)
72
67
buffer[i++] = 127 ;
73
68
buffer[i++] = 0x00 ;
74
69
buffer[i++] = 0x00 ;
75
70
buffer[i++] = 0x00 ;
76
71
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 );
81
76
}
82
77
83
78
// send header
84
79
client->tcp .write (&buffer[0 ], i);
85
80
86
- if (payload && lenght > 0 ) {
81
+ if (payload && length > 0 ) {
87
82
// send payload
88
- client->tcp .write (&payload[0 ], lenght );
83
+ client->tcp .write (&payload[0 ], length );
89
84
}
90
85
91
86
}
@@ -136,7 +131,7 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
136
131
}
137
132
payloadLen = buffer[0 ] << 8 | buffer[1 ];
138
133
} else if (payloadLen == 127 ) {
139
- // read 64bit inteager as Lenght
134
+ // read 64bit inteager as length
140
135
if (!readWait (client, buffer, 8 )) {
141
136
// timeout
142
137
clientDisconnect (client, 1002 );
@@ -191,32 +186,24 @@ void WebSockets::handleWebsocket(WSclient_t * client) {
191
186
192
187
switch (opCode) {
193
188
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!
202
191
case WSop_binary:
203
- // todo API for user to get message may callback
192
+ messageRecived (client, opCode, payload, payloadLen);
204
193
break ;
205
194
case WSop_ping:
206
- // todo send pong
195
+ // send pong back
196
+ sendFrame (client, WSop_pong, payload, payloadLen);
207
197
break ;
208
198
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);
211
200
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
+ }
220
207
break ;
221
208
case WSop_continuation:
222
209
// continuation is not supported
0 commit comments