AsyncWebSocket printf bug
I found a bug in AsyncWebSocket printf function: wrong argument printed, perhaps invalid pointer. The problem seems to happen with
const char WSErr[] PROGMEM = "{\"type\":\"STATUS\",\"response\":\"Error at %d\"}";
int clientID;
bool readyToTest = false;
AsyncWebSocket ws("/.ws"); // access at ws://[esp ip]/.ws
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)
{
//Handle WebSocket event
if(type == WS_EVT_CONNECT) {
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf("{\"type\":\"ACK\",\"response\":\"HELO\",\"client-id\":%u}", client->id());
client->ping();
clientID = client->id();
} else if(type == WS_EVT_DISCONNECT) {
Serial.printf("ws[%s][%u] disconnect: u\n", server->url(), client->id());
} else if(type == WS_EVT_ERROR) {
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
} else if(type == WS_EVT_PONG) {
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
readyToTest = true;
} else if(type == WS_EVT_DATA) {
}
}
void testWS() {
char tmp[100];
unsigned char testvalue = 2;
strcpy_P(tmp, WSErr);
Serial.printf(tmp, testvalue);
ws.printf(clientID, tmp, testvalue);
}
void loop {
if(readyToTest) {
readyToTest = false;
testWS();
}
}
Output at serial monitor:
{"type":"STATUS","response":"Error at 2"}
Output at browser console (see script below)
{"type":"STATUS","response":"Error at 1073698976"}
This is the javascript that handles websocket connections and report to console:
ws = new WebSocket("ws://10.0.22.101/.ws", WSPROTO);
ws.onopen = function(event) {
connected = true;
};
ws.onclose = function(event) {
connected = false;
};
ws.onmessage = function (event) {
console.log(event.data);
}
Yes I stumbled upon this issue in another library and meant to look at my code here, but got busy with other things and forgot :) Thanks for noting, will fix.
Happy to be helpful, thanks to you.
Just noting that this is still an issue. At least maybe an update to the docs noting this? I spent a long time trying to figure out why ws.printf(client_id... was giving me very strange output when ws.printfAll(... worked fine.
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.
[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.