The event handler is looping

42 views
Skip to first unread message

Johan

unread,
May 23, 2015, 4:46:01 PM5/23/15
to mongoos...@googlegroups.com

Hi I am working on an embedded server using mongoose server. I have the following code fromMongoose Doc.

#include <string.h>
#include "mongoose.h"

static int event_handler(struct mg_connection *conn, enum mg_event ev) {
  if (ev == MG_AUTH) {
    return MG_TRUE;   // Authorize all requests
  } else if (ev == MG_REQUEST && !strcmp(conn->uri, "/hello")) {
      mg_printf_data(conn, "%s", "Hello world");
    return MG_TRUE;   // Mark as processed
  } else {
    return MG_FALSE;  // Rest of the events are not processed
  }
}

int main(void) {
  struct mg_server *server = mg_create_server(NULL, event_handler);
  //mg_set_option(server, "document_root", ".");
  mg_set_option(server, "listening_port", "8080");

  for (;;) {
    mg_poll_server(server, 1000);  // Infinite loop, Ctrl-C to stop
  }
  mg_destroy_server(&server);

  return 0;
}

This code work perfectly. After sending "Hello World", MG_POLL stops. and chrome shows loading finished. But when I try to send the error code by replacing the mg_printf with 

mg_send_status(conn, 400);
mg_send_header(conn, "Content-Type", "text/plain");
mg_send_data(conn, conn->content, conn->content_len);
mg_send_data(conn, "", 0);

chrome shows loading finished. But the event_handler is getting called repeatedly. It looks like not getting CLOSE. when I try closing the browser window the connection is getting closed and call back stops? Is there any thing more I need to do to fix the problem

Sergey Lyubka

unread,
May 27, 2015, 12:45:04 PM5/27/15
to mongoose-users
I cannot reproduce the problem - everything works as expected.
What version of Mongoose are you using?

--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages