Skip to content

Commit f1ecfa9

Browse files
committed
first full working version of WebSocketsServer
1 parent cc60722 commit f1ecfa9

File tree

6 files changed

+265
-59
lines changed

6 files changed

+265
-59
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* WebSocketsServer.ino
3+
*
4+
* Created on: 22.05.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
#include <WebSocketsServer.h>
13+
#include <hash.h>
14+
15+
ESP8266WiFiMulti WiFiMulti;
16+
17+
WebSocketsServer webSocket = WebSocketsServer(81);
18+
19+
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
20+
21+
switch(type) {
22+
case WStype_DISCONNECTED:
23+
Serial1.printf("[%d] Disconnected!\n", num);
24+
break;
25+
case WStype_CONNECTED:
26+
Serial1.printf("[%d] Connected.\n", num);
27+
break;
28+
case WStype_TEXT:
29+
Serial1.printf("[%d] get Text: %s\n", num, payload);
30+
31+
// echo data back to browser
32+
webSocket.sendTXT(num, payload, lenght);
33+
34+
// send data to all connected clients
35+
webSocket.broadcastTXT(payload, lenght);
36+
break;
37+
case WStype_BIN:
38+
Serial1.printf("[%d] get binary.\n", num);
39+
hexdump(payload, lenght);
40+
41+
// echo data back to browser
42+
webSocket.sendBIN(num, payload, lenght);
43+
break;
44+
}
45+
46+
}
47+
48+
void setup() {
49+
Serial.begin(921600);
50+
Serial1.begin(921600);
51+
52+
//Serial.setDebugOutput(true);
53+
Serial1.setDebugOutput(true);
54+
55+
Serial1.println();
56+
Serial1.println();
57+
Serial1.println();
58+
59+
for(uint8_t t = 4; t > 0; t--) {
60+
Serial1.printf("[SETUP] BOOT WAIT %d...\n", t);
61+
Serial1.flush();
62+
delay(1000);
63+
}
64+
65+
WiFiMulti.addAP("SSID", "passpasspass");
66+
67+
while(WiFiMulti.run() != WL_CONNECTED) {
68+
delay(100);
69+
}
70+
71+
webSocket.begin();
72+
webSocket.onEvent(webSocketEvent);
73+
}
74+
75+
void loop() {
76+
webSocket.loop();
77+
}
78+

src/WebSockets.cpp

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@
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
*/
3434
void 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

src/WebSockets.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#endif
3838
#endif
3939

40-
#define DEBUG_WEBSOCKETS(...) Serial1.printf( __VA_ARGS__ ); Serial1.flush()
40+
//#define DEBUG_WEBSOCKETS(...) Serial1.printf( __VA_ARGS__ );
4141

4242
#ifndef DEBUG_WEBSOCKETS
4343
#define DEBUG_WEBSOCKETS(...)
@@ -95,9 +95,10 @@ class WebSockets {
9595
virtual void clientDisconnect(WSclient_t * client);
9696
virtual bool clientIsConnected(WSclient_t * client);
9797

98+
virtual void messageRecived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length);
9899

99100
void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
100-
void sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t lenght = 0);
101+
void sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0);
101102

102103

103104
void handleWebsocket(WSclient_t * client);

0 commit comments

Comments
 (0)