Skip to content

Commit 3e50a74

Browse files
committed
Removed unnecessary variables, globals. Cleaned out CRLF from saved data and got rid of ugly patches later on thanks to that.
1 parent d9574eb commit 3e50a74

File tree

4 files changed

+27
-112
lines changed

4 files changed

+27
-112
lines changed

WebSocket.cpp

Lines changed: 22 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33

44
#define DEBUGGING
55

6-
WebSocket::WebSocket(byte ip[], const char *urlPrefix, int inPort) :
6+
WebSocket::WebSocket(const char *urlPrefix, int inPort) :
77
socket_server(inPort),
88
socket_client(255),
99
socket_actions_population(0),
1010
socket_urlPrefix(urlPrefix)
1111
{
12-
ipAddress[0]=ip[0];
13-
ipAddress[1]=ip[1];
14-
ipAddress[2]=ip[2];
15-
ipAddress[3]=ip[3];
16-
port=inPort;
1712
}
1813

1914
void WebSocket::begin() {
@@ -75,13 +70,13 @@ bool WebSocket::analyzeRequest(int bufferLength) {
7570
// OK, it's a websockets handshake for sure
7671
foundupgrade = true;
7772
} else if (temp.startsWith("Origin: ")) {
78-
origin = temp.substring(8,temp.length());
73+
origin = temp.substring(8,temp.length() - 2); // Don't save last CR+LF
7974
} else if (temp.startsWith("Host: ")) {
80-
host = temp.substring(6,temp.length());
75+
host = temp.substring(6,temp.length() - 2); // Don't save last CR+LF
8176
} else if (temp.startsWith("Sec-WebSocket-Key1")) {
82-
key[0]=temp.substring(20,temp.length());
77+
key[0]=temp.substring(20,temp.length() - 2); // Don't save last CR+LF
8378
} else if (temp.startsWith("Sec-WebSocket-Key2")) {
84-
key[1]=temp.substring(20,temp.length());
79+
key[1]=temp.substring(20,temp.length() - 2); // Don't save last CR+LF
8580
}
8681
temp = "";
8782
}
@@ -94,8 +89,8 @@ bool WebSocket::analyzeRequest(int bufferLength) {
9489
if (foundupgrade == true && host.length() > 0 && key[0].length() > 0 && key[1].length() > 0) {
9590
// All ok, proceed with challenge and MD5 digest
9691
char key3[9] = {0};
97-
// The last 8 bytes of temp should contain the third key
98-
temp.toCharArray(key3, 9); // TODO: 8 only?
92+
// What now is in temp should be the third key
93+
temp.toCharArray(key3, 9);
9994

10095
// Process keys
10196
for (int i = 0; i <= 1; i++) {
@@ -117,8 +112,6 @@ bool WebSocket::analyzeRequest(int bufferLength) {
117112
}
118113

119114
unsigned char challenge[16] = {0};
120-
121-
// Big Endian
122115
challenge[0] = (unsigned char) ((intkey[0] >> 24) & 0xFF);
123116
challenge[1] = (unsigned char) ((intkey[0] >> 16) & 0xFF);
124117
challenge[2] = (unsigned char) ((intkey[0] >> 8) & 0xFF);
@@ -131,45 +124,27 @@ bool WebSocket::analyzeRequest(int bufferLength) {
131124
memcpy(challenge + 8, key3, 8);
132125

133126
unsigned char md5Digest[16];
134-
135127
MD5(challenge, md5Digest, 16);
136128

137129
#ifdef DEBUGGING
138130
Serial.println("Sending response header");
139131
#endif
140-
socket_client.write("HTTP/1.1 101 Web Socket Protocol Handshake");
141-
socket_client.write(CRLF);
142-
socket_client.write("Upgrade: WebSocket");
143-
socket_client.write(CRLF);
144-
socket_client.write("Connection: Upgrade");
145-
socket_client.write(CRLF);
146-
socket_client.write("Sec-WebSocket-Origin: ");
147-
148-
// TODO: Fix this mess
149-
char corigin[origin.length() - 1];
150-
origin.toCharArray(corigin, origin.length() - 1);
151-
socket_client.write(corigin);
152-
socket_client.write(CRLF);
153-
154-
// TODO: Clean up this mess!
155-
156-
//Assign buffer for conversions
157-
char buf[10];
132+
socket_client.print("HTTP/1.1 101 Web Socket Protocol Handshake\r\n");
133+
socket_client.print("Upgrade: WebSocket\r\n");
134+
socket_client.print("Connection: Upgrade\r\n");
135+
socket_client.print("Sec-WebSocket-Origin: ");
136+
socket_client.print(origin);
137+
socket_client.print(CRLF);
158138

159139
// The "Host:" value should be used as location
160-
socket_client.write("Sec-WebSocket-Location: ws://");
161-
char cHost[host.length() - 1];
162-
host.toCharArray(cHost, host.length() - 1);
163-
socket_client.write(cHost);
164-
socket_client.write(socket_urlPrefix);
165-
socket_client.write(CRLF);
166-
socket_client.write(CRLF);
140+
socket_client.print("Sec-WebSocket-Location: ws://");
141+
socket_client.print(host);
142+
socket_client.print(socket_urlPrefix);
143+
socket_client.print(CRLF);
144+
socket_client.print(CRLF);
167145

168146
socket_client.write(md5Digest, 16);
169147

170-
171-
// Need to keep global state
172-
socket_reading = true;
173148
return true;
174149
} else {
175150
// Nope, failed handshake. Disconnect
@@ -214,64 +189,6 @@ void WebSocket::socketStream(int socketBufferLength) {
214189
}
215190
}
216191

217-
/*
218-
void WebSocket::socketStream(int socketBufferLength) {
219-
while (socket_reading) {
220-
char bite;
221-
int frameLength = 0;
222-
// String to hold bytes sent by client to server.
223-
String socketString = String(socketBufferLength);
224-
// Timeout timeframe variable.
225-
unsigned long timeoutTime = millis() + TIMEOUT_IN_MS;
226-
227-
if (!socket_client.connected()) {
228-
#ifdef DEBUGGING
229-
Serial.println("No connection!");
230-
#endif
231-
socket_reading = false;
232-
return;
233-
}
234-
235-
// While there is a client stream to read...
236-
while ((bite = socket_client.read()) && socket_reading) {
237-
if (bite == 0)
238-
continue; // Don't save this byte
239-
if ((uint8_t) bite == 0xFF) {
240-
// Frame end
241-
// Timeout check.
242-
unsigned long currentTime = millis();
243-
if ((currentTime > timeoutTime) && !socket_client.connected()) {
244-
#ifdef DEBUGGING
245-
Serial.println("Connection timed out");
246-
#endif
247-
disconnectStream();
248-
return;
249-
} else {
250-
break; // Break out and process the frame
251-
}
252-
}
253-
254-
// Append everything but Frame Start and Frame End to socketString
255-
socketString += bite;
256-
if (++frameLength > MAX_FRAME_LENGTH) {
257-
// Too big to handle! Abort and disconnect.
258-
#ifdef DEBUGGING
259-
Serial.print("Client send frame exceeding ");
260-
Serial.print(MAX_FRAME_LENGTH);
261-
Serial.print("bytes");
262-
#endif
263-
disconnectStream();
264-
socket_reading = false;
265-
return;
266-
}
267-
}
268-
269-
// Process the String.
270-
executeActions(socketString);
271-
}
272-
}
273-
*/
274-
275192
void WebSocket::addAction(Action *socketAction) {
276193
#ifdef DEBUGGING
277194
Serial.println("Adding actions");
@@ -286,10 +203,10 @@ void WebSocket::disconnectStream() {
286203
Serial.println("Terminating socket");
287204
#endif
288205
// Should send 0xFF00 to client to tell it I'm quitting here.
206+
// TODO: Check if I understood this properly
289207
socket_client.write((uint8_t) 0xFF);
290208
socket_client.write((uint8_t) 0x00);
291209

292-
socket_reading = false;
293210
socket_client.flush();
294211
delay(1);
295212
socket_client.stop();
@@ -311,8 +228,8 @@ void WebSocket::sendData(const char *str) {
311228
Serial.println(str);
312229
#endif
313230
if (socket_client.connected()) {
314-
socket_client.write((uint8_t) 0x00); // Frame start
315-
socket_client.write(str);
316-
socket_client.write((uint8_t) 0xFF); // Frame end
231+
socket_client.print((uint8_t) 0x00); // Frame start
232+
socket_client.print(str);
233+
socket_client.print((uint8_t) 0xFF); // Frame end
317234
}
318235
}

WebSocket.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Currently based off of "The Web Socket protocol" draft (v 75):
7373
class WebSocket {
7474
public:
7575
// Constructor for websocket class.
76-
WebSocket(uint8_t *ip, const char *urlPrefix = "/", int port = 80);
76+
WebSocket(const char *urlPrefix = "/", int inPort = 80);
7777

7878
// Processor prototype. Processors allow the websocket server to
7979
// respond to input from client based on what the client supplies.
@@ -101,10 +101,6 @@ class WebSocket {
101101
Client socket_client;
102102

103103
const char *socket_urlPrefix;
104-
byte ipAddress[4];
105-
int port;
106-
107-
bool socket_reading;
108104

109105
String origin;
110106
String host;

examples/Websocket_Actions/Websocket_Actions.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ byte mac[] = { 0x52, 0x4F, 0x43, 0x4B, 0x45, 0x54 };
1717
byte ip[] = { 192, 168, 4, 2 };
1818

1919
// Create a Websocket server listening to http://192.168.4.2:8080/ws/
20-
WebSocket websocketServer(ip, PREFIX, PORT);
20+
WebSocket websocketServer(PREFIX, PORT);
2121

2222
// You must have at least one function with the following signature.
2323
// It will be called by the server when a data frame is received.
@@ -44,6 +44,7 @@ void setup() {
4444
// add them to the server. If you have more than one, define CALLBACK_FUNCTIONS before including
4545
// WebSocket.h
4646
websocketServer.addAction(&dataReceivedAction);
47+
delay(1000); // Give Ethernet time to get ready
4748
}
4849

4950
void loop() {

examples/Websocket_Demo/Websocket_Demo.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ byte mac[] = { 0x52, 0x4F, 0x43, 0x4B, 0x45, 0x54 };
1414
byte ip[] = { 192, 168, 1, 170 };
1515

1616
// Create a Websocket server listening to http://192.168.1.170/
17-
WebSocket websocketServer(ip);
17+
WebSocket websocketServer();
1818

1919
// You must have at least one function with the following signature.
2020
// It will be called by the server when a data frame is received.
@@ -34,6 +34,7 @@ void setup() {
3434
// add them to the server. If you have more than one, define CALLBACK_FUNCTIONS before including
3535
// WebSocket.h
3636
websocketServer.addAction(&dataReceivedAction);
37+
delay(1000); // Give Ethernet time to get ready
3738
}
3839

3940
void loop() {

0 commit comments

Comments
 (0)