I am encountering an issue where extra characters are being added to the command when sending it from WebSocat to my WebSocket server implemented in C using libwebsockets.
Here’s my WebSocket server code:
#include <libwebsockets.h>
#include <string.h>
#include <stdio.h>
static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len) {
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
printf("Connection establishedn");
break;
case LWS_CALLBACK_RECEIVE:
printf("Message received: %sn", (char *)in);
break;
default:
printf("Unhandled callback reason: %dn", reason);
break;
}
return 0;
}
static struct lws_protocols protocols[] = {
{ "ws", callback_ws, 0, 1024 },
{ NULL, NULL, 0, 0 } // Terminator
};
int main(void) {
struct lws_context_creation_info info;
memset(&info, 0, sizeof info);
info.port = 8080;
info.protocols = protocols;
struct lws_context *context = lws_create_context(&info);
if (context == NULL) {
fprintf(stderr, "lws init failedn");
return -1;
}
printf("Starting server...n");
while (1) {
lws_service(context, 1000); // 1000 ms = 1 second
}
lws_context_destroy(context);
return 0;
}
When I send a command, such as “open”, from WebSocat to the WebSocket server, I expect to receive only “open” on the server side. However, I observe additional characters appended to the command, for example, “openWiTG8mQpA4myt1aTI=”. This issue prevents me from properly processing the commands on the server side.
I suspect that these extra characters might be coming from a formatting issue or an encoding problem between WebSocat and the WebSocket server. Can anyone suggest a solution or provide insights into why this might be happening?