diff --git a/WSClient.cpp b/WSClient.cpp index d19bad2..55a46d0 100755 --- a/WSClient.cpp +++ b/WSClient.cpp @@ -23,7 +23,7 @@ // THE SOFTWARE. #include "sha1.h" -#include "base64.h" +#include "Base64.h" #include #include @@ -342,17 +342,15 @@ char* WSClient::getData() { return (char*)socketStr; } -void WSClient::sendData(char *str) { -// Serial.println(F("")); Serial.print(F("TX: ")); -// for (int i=0; iconnected()) { - sendEncodedData(str); + sendEncodedData(s); } } - - int WSClient::timedRead() { while (!socket_client->available()) { delay(50); @@ -362,33 +360,39 @@ int a = socket_client->read(); return a; } -void WSClient::sendEncodedData(char *str) { - int size = strlen(str); - - // string type - socket_client->write(0x81); - - // NOTE: no support for > 16-bit sized messages - if (size > 125) { - //Serial.println(F("size bigger than 125")); - socket_client->write(127); - socket_client->write((uint8_t) (size >> 56) & 255); - socket_client->write((uint8_t) (size >> 48) & 255); - socket_client->write((uint8_t) (size >> 40) & 255); - socket_client->write((uint8_t) (size >> 32) & 255); - socket_client->write((uint8_t) (size >> 24) & 255); - socket_client->write((uint8_t) (size >> 16) & 255); - socket_client->write((uint8_t) (size >> 8) & 255); - socket_client->write((uint8_t) (size ) & 255); -} else { - //Serial.println(F("size small than 125")); - socket_client->write((uint8_t) size); -} +bool WSClient::sendEncodedData(String s) { + int totalSize = 6 + (s.length() < 126 ? 0 : s.length() < 2^16 ? 2 : 8) + s.length(); + if (totalSize > 400){//limited by static toSend array size + Serial.println("string too long: "); + Serial.println(s); + Serial.println(totalSize); + return false; + } + uint8_t toSend[totalSize]; + toSend[0] = B10000001;//FIN,RSV1,RSV2,RSV3,OPCODE(4) + toSend[1] = B10000000 + (s.length() < 126 ? s.length() : (s.length() < 2^16 ? 126 : 127));//MASK, LEN(7) + int i = 2; + if (s.length() < 126){ + //don't do anything + } else if (s.length() < 2^16){ + toSend[2] = (uint8_t)((s.length() >> 8) & 0xff); + toSend[3] = (uint8_t)(s.length() & 0xff); + i = 4; + } else { + //we should never get here, since this would mean the String is using 65k of memory + Serial.println("shouldn't be here"); + } -for (int i=0; iwrite(str[i]); -} + for(int n = 0; n < 4; n++, i++){ + toSend[i] = B00000000;//MASK_KEY(8) + } + for(int n = 0; n < s.length(); n++, i++){ + toSend[i] = s.charAt(n) ^ B00000000; + } + socket_client->write(toSend, totalSize); + + return true; } diff --git a/WSClient.h b/WSClient.h index f34ae9f..9be3dc4 100755 --- a/WSClient.h +++ b/WSClient.h @@ -38,12 +38,12 @@ class WSClient { bool handshake(Client &client); // Handle connection requests to validate and process/refuse connections. char* getData(); // Get data off of the stream - void sendData(char *str); // Write data to the stream + void sendData(String s); // Write data to the stream void disconnect(); char *path; char *host; - void sendEncodedData(char *str); + bool sendEncodedData(String s); private: