I'm using libwebsockets-2.1.0 with generic session
& lwsws
option enabled.
In case LWS_CALLBACK_SERVER_WRITEABLE
I've got some code that opens a file, and output the content to websocket.
static const char* filename = "/tmp/loop.log";
#define MAX_STAT_LINE_LENGTH 256
unsigned char buf[LWS_PRE + 512];
unsigned char *p = &buf[LWS_PRE];
char line[MAX_STAT_LINE_LENGTH];
while ( fgets(line, sizeof(line), fp) != NULL) {
int n = lws_snprintf((char *)p, sizeof(line), "%s", line);
int m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
if (m < n) {
printf("websocket write failed\n");
}
}
In the terminal I'm getting a bunch of these:
lwsws[13778]: ****** 0x9230a50: Sending new 46 (/name?ag=z&abcd=011), pending truncated ...
It's illegal to do an lws_write outside of the writable callback: fix your code
Is there an explanation for this error? I mean, I have declared char line[1000]
but it's still complaining.
I would like to point out that the final output of the websocket is inconsistent, sometimes it stops at line 30-something
, sometimes it stops at line 400
FWIW, the total line of the file I'm reading is 1758 lines, with the longest character length of a line is 107 characters long.
Removing the fgets
loop and replacing the value with my own generic value seems to work fine.
Thanks