Statistics
| Branch: | Tag: | Revision:

mongoose / examples / timers / timers.c @ eaef5bd1

History | View | Annotate | Download (1.27 KB)

1
// Copyright (c) 2015 Cesanta Software Limited
2
// All rights reserved
3

    
4
#include "mongoose.h"
5

    
6
static const char *s_http_port = "8000";
7
static struct mg_serve_http_opts s_http_server_opts;
8

    
9
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
10
  switch (ev) {
11
    case MG_EV_HTTP_REQUEST:
12
      mg_serve_http(c, (struct http_message *) ev_data, s_http_server_opts);
13
      break;
14
    case MG_EV_TIMER: {
15
      double now = *(double *) ev_data;
16
      double next = mg_set_timer(c, 0) + 0.5;
17
      printf("timer event, current time: %.2lf\n", now);
18
      mg_set_timer(c, next);  // Send us timer event again after 0.5 seconds
19
      break;
20
    }
21
  }
22
}
23

    
24
int main(void) {
25
  struct mg_mgr mgr;
26
  struct mg_connection *c;
27

    
28
  mg_mgr_init(&mgr, NULL);
29
  c = mg_bind(&mgr, s_http_port, ev_handler);
30
  if (c == NULL) {
31
    printf("Cannot start on port %s\n", s_http_port);
32
    return EXIT_FAILURE;
33
  }
34

    
35
  // Set up HTTP server parameters
36
  mg_set_protocol_http_websocket(c);
37
  s_http_server_opts.document_root = ".";  // Serve current directory
38

    
39
  // Send us MG_EV_TIMER event after 2.5 seconds
40
  mg_set_timer(c, mg_time() + 2.5);
41

    
42
  printf("Starting on port %s, time: %.2lf\n", s_http_port, mg_time());
43
  for (;;) {
44
    mg_mgr_poll(&mgr, 1000);
45
  }
46
  mg_mgr_free(&mgr);
47

    
48
  return EXIT_SUCCESS;
49
}