Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/WebSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ void WebSockets::clientDisconnect(WSclient_t * client, uint16_t code, char * rea
* @param opcode WSopcode_t
* @param payload uint8_t * ptr to the payload
* @param length size_t length of the payload
* @param mask bool add dummy mask to the frame (needed for web browser)
* @param fin bool can be used to send data in more then one frame (set fin on the last frame)
* @param headerToPayload bool set true if the payload has reserved 14 Byte at the beginning to dynamically add the Header (payload neet to be in RAM!)
* @return true if ok
*/
bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool mask, bool fin, bool headerToPayload) {
bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin, bool headerToPayload) {

if(client->tcp && !client->tcp->connected()) {
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] not Connected!?\n", client->num);
Expand All @@ -95,7 +94,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
}

DEBUG_WEBSOCKETS("[WS][%d][sendFrame] ------- send message frame -------\n", client->num);
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] fin: %u opCode: %u mask: %u length: %u headerToPayload: %u\n", client->num, fin, opcode, mask, length, headerToPayload);
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] fin: %u opCode: %u mask: %u length: %u headerToPayload: %u\n", client->num, fin, opcode, client->cIsClient, length, headerToPayload);

if(opcode == WSop_text) {
DEBUG_WEBSOCKETS("[WS][%d][sendFrame] text: %s\n", client->num, (payload + (headerToPayload ? 14 : 0)));
Expand All @@ -119,7 +118,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
headerSize = 10;
}

if(mask) {
if(client->cIsClient) {
headerSize += 4;
}

Expand Down Expand Up @@ -158,7 +157,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay

// byte 1
*headerPtr = 0x00;
if(mask) {
if(client->cIsClient) {
*headerPtr |= bit(7); ///< set mask
}

Expand Down Expand Up @@ -194,7 +193,7 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
headerPtr++;
}

if(mask) {
if(client->cIsClient) {
if(useInternBuffer) {
// if we use a Intern Buffer we can modify the data
// by this fact its possible the do the masking
Expand Down Expand Up @@ -434,7 +433,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
break;
case WSop_ping:
// send pong back
sendFrame(client, WSop_pong, payload, header->payloadLen, true);
sendFrame(client, WSop_pong, payload, header->payloadLen);
break;
case WSop_pong:
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload ? (const char*)payload : "");
Expand Down
3 changes: 2 additions & 1 deletion src/WebSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ typedef struct {
String cUrl; ///< http url
uint16_t cCode; ///< http code

bool cIsClient = false; ///< will be used for masking
bool cIsUpgrade; ///< Connection == Upgrade
bool cIsWebsocket; ///< Upgrade == websocket

Expand Down Expand Up @@ -285,7 +286,7 @@ class WebSockets {
virtual void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) = 0;

void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool mask = false, bool fin = true, bool headerToPayload = false);
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool fin = true, bool headerToPayload = false);

void headerDone(WSclient_t * client);

Expand Down
7 changes: 4 additions & 3 deletions src/WebSocketsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
WebSocketsClient::WebSocketsClient() {
_cbEvent = NULL;
_client.num = 0;
_client.cIsClient = true;
_client.extraHeaders = WEBSOCKETS_STRING("Origin: file://");
}

Expand Down Expand Up @@ -194,7 +195,7 @@ bool WebSocketsClient::sendTXT(uint8_t * payload, size_t length, bool headerToPa
length = strlen((const char *) payload);
}
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_text, payload, length, true, true, headerToPayload);
return sendFrame(&_client, WSop_text, payload, length, true, headerToPayload);
}
return false;
}
Expand Down Expand Up @@ -225,7 +226,7 @@ bool WebSocketsClient::sendTXT(String & payload) {
*/
bool WebSocketsClient::sendBIN(uint8_t * payload, size_t length, bool headerToPayload) {
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_binary, payload, length, true, true, headerToPayload);
return sendFrame(&_client, WSop_binary, payload, length, true, headerToPayload);
}
return false;
}
Expand All @@ -242,7 +243,7 @@ bool WebSocketsClient::sendBIN(const uint8_t * payload, size_t length) {
*/
bool WebSocketsClient::sendPing(uint8_t * payload, size_t length) {
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_ping, payload, length, true);
return sendFrame(&_client, WSop_ping, payload, length);
}
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/WebSocketsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ bool WebSocketsServer::sendTXT(uint8_t num, uint8_t * payload, size_t length, bo
}
WSclient_t * client = &_clients[num];
if(clientIsConnected(client)) {
return sendFrame(client, WSop_text, payload, length, false, true, headerToPayload);
return sendFrame(client, WSop_text, payload, length, true, headerToPayload);
}
return false;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ bool WebSocketsServer::broadcastTXT(uint8_t * payload, size_t length, bool heade
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i];
if(clientIsConnected(client)) {
if(!sendFrame(client, WSop_text, payload, length, false, true, headerToPayload)) {
if(!sendFrame(client, WSop_text, payload, length, true, headerToPayload)) {
ret = false;
}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ bool WebSocketsServer::sendBIN(uint8_t num, uint8_t * payload, size_t length, bo
}
WSclient_t * client = &_clients[num];
if(clientIsConnected(client)) {
return sendFrame(client, WSop_binary, payload, length, false, true, headerToPayload);
return sendFrame(client, WSop_binary, payload, length, true, headerToPayload);
}
return false;
}
Expand All @@ -285,7 +285,7 @@ bool WebSocketsServer::broadcastBIN(uint8_t * payload, size_t length, bool heade
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i];
if(clientIsConnected(client)) {
if(!sendFrame(client, WSop_binary, payload, length, false, true, headerToPayload)) {
if(!sendFrame(client, WSop_binary, payload, length, true, headerToPayload)) {
ret = false;
}
}
Expand Down