static void ICACHE_FLASH_ATTR wsRecvCb(void *esp_connection, char *data, unsigned short len) { char *key = (char *)os_strstr(data, WS_KEY_IDENTIFIER); WSConnection *wsConnection = getWsConnection(esp_connection); /*if (wsConnection == NULL) { //huh? return; }*/ //get the first occurrence of the key identifier if (key != NULL) { // ------------------------ Handle the Handshake ------------------------ //Skip the identifier (that contains the space already) key += os_strlen(WS_KEY_IDENTIFIER); //the key ends at the newline char *endSequence = (char *)os_strstr(key, HTML_HEADER_LINEEND); if (endSequence != NULL) { int keyLastChar = endSequence - key; //we can throw away all the other data, only the key is interesting key[keyLastChar] = '\0'; char acceptKey[100]; createWsAcceptKey(key, acceptKey, 100); //now construct our message and send it back to the client char responseMessage[strlen(WS_RESPONSE) + 100]; os_sprintf(responseMessage, WS_RESPONSE, acceptKey); //send the response espconn_sent(esp_connection, (uint8_t *)responseMessage, strlen(responseMessage)); wsConnection->status = STATUS_OPEN; //call the connection callback if (wsOnConnectionCallback != NULL) { wsOnConnectionCallback(wsConnection); } } } else { // ------------------------ Handle a Frame ------------------------ WSFrame frame; parseWsFrame(data, &frame); if (frame.isMasked) { unmaskWsPayload(frame.payloadData, frame.payloadLength, frame.maskingKey); } else { //we are the server, and need to shut down the connection //if we receive an unmasked packet closeWsConnection(wsConnection); return; } if (frame.opcode == OPCODE_PING) { sendWsMessage(wsConnection, frame.payloadData, frame.payloadLength, FLAG_FIN | OPCODE_PONG); return; } if(frame.opcode == OPCODE_CLOSE){ //gracefully shut down the connection closeWsConnection(wsConnection); return; } if(wsConnection->onMessage != NULL){ wsConnection->onMessage(wsConnection, &frame); } } }
//*********************************************************************** void ICACHE_FLASH_ATTR webSocketRecvCb(void *arg, char *data, unsigned short len) { espconn *esp_connection = (espconn*)arg; //received some data from webSocket connection //webSocketDebug("In webSocketRecvCb\n"); // webSocketDebug("webSocket recv--->%s<----\n", data); WSConnection *wsConnection = getWsConnection(esp_connection); if (wsConnection == NULL) { webSocketDebug("webSocket Heh?\n"); return; } //get the first occurrence of the key identifier char *key = os_strstr(data, WS_KEY_IDENTIFIER); // webSocketDebug("key-->%s<--\n", key ); if (key != NULL) { // ------------------------ Handle the Handshake ------------------------ // webSocketDebug("In Handle the Handshake\n"); //Skip the identifier (that contains the space already) key += os_strlen(WS_KEY_IDENTIFIER); // webSocketDebug("keynow-->%s<--\n", key); //the key ends at the newline char *endSequence = os_strstr(key, HTML_HEADER_LINEEND); // webSocketDebug("endSequency-->%s<--\n", endSequence); if (endSequence != NULL) { int keyLastChar = endSequence - key; //we can throw away all the other data, only the key is interesting key[keyLastChar] = '\0'; // webSocketDebug("keyTrimmed-->%s<--\n", key); char acceptKey[100]; createWsAcceptKey(key, acceptKey, 100); // webSocketDebug("acceptKey-->%s<--\n", acceptKey); //now construct our message and send it back to the client char responseMessage[strlen(WS_RESPONSE) + 100]; os_sprintf(responseMessage, WS_RESPONSE, acceptKey); // webSocketDebug("responseMessage-->%s<--\n", responseMessage); //send the response espconn_sent(esp_connection, (uint8_t *)responseMessage, strlen(responseMessage)); wsConnection->status = STATUS_OPEN; //call the connection callback if (wsOnConnectionCallback != NULL) { // webSocketDebug("Handle the Handshake 5\n"); wsOnConnectionCallback(); } } } else { // ------------------------ Handle a Frame ------------------------ // webSocketDebug("In Handle a Frame\n"); WSFrame frame; parseWsFrame(data, &frame); if (frame.isMasked) { unmaskWsPayload(frame.payloadData, frame.payloadLength, frame.maskingKey); } else { //we are the server, and need to shut down the connection //if we receive an unmasked packet // webSocketDebug("frame.isMasked=false closing connection\n"); closeWsConnection(wsConnection); return; } // webSocketDebug("frame.payloadData-->%s<--\n", frame.payloadData); if (frame.opcode == OPCODE_PING) { // webSocketDebug("frame.opcode=OPCODE_PING\n"); sendWsMessage(wsConnection, frame.payloadData, frame.payloadLength, FLAG_FIN | OPCODE_PONG); return; } if (frame.opcode == OPCODE_CLOSE) { //gracefully shut down the connection // webSocketDebug("frame.opcode=OPCODE_CLOSE, closeing connection\n"); closeWsConnection(wsConnection); return; } if (wsConnection->onMessage != NULL) { wsConnection->onMessage(frame.payloadData); } } // webSocketDebug("Leaving webSocketRecvCb\n"); }