Commit e84d0574 authored by Sergey Lyubka's avatar Sergey Lyubka

Fixed websocket example logic when freeing the message

parent 1a64d37f
......@@ -3,6 +3,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"
static int event_handler(struct mg_event *event) {
......@@ -25,14 +26,16 @@ static int event_handler(struct mg_event *event) {
mg_websocket_write(event->conn, WEBSOCKET_OPCODE_TEXT,
server_ready_message, strlen(server_ready_message));
// Read messages sent by client. Echo them back.
while ((len = mg_websocket_read(event->conn, &bits, &data)) > 0) {
// Echo message back to the client
mg_websocket_write(event->conn, WEBSOCKET_OPCODE_TEXT, data, len);
printf("got message: [%.*s]\n", len, data);
mg_websocket_write(event->conn, WEBSOCKET_OPCODE_TEXT, data, len);
free(data); // It's our responsibility to free allocated message
// If the message is "exit", close the connection, exit the loop
if (memcmp(data, "exit", 4) == 0) {
mg_websocket_write(event->conn,
WEBSOCKET_OPCODE_CONNECTION_CLOSE, "", 0);
free(data);
break;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment