2014-09-09 16:27:35 +08:00
|
|
|
// Copyright (c) 2013-2014 Cesanta Software Limited
|
2014-09-10 01:07:55 +08:00
|
|
|
// $Date: 2014-09-09 17:07:55 UTC $
|
2014-09-09 16:27:35 +08:00
|
|
|
|
2014-01-17 19:45:57 +08:00
|
|
|
#include <string.h>
|
2014-03-02 20:16:09 +08:00
|
|
|
#include <time.h>
|
2012-09-19 19:31:19 +08:00
|
|
|
#include "mongoose.h"
|
|
|
|
|
2014-09-09 16:27:35 +08:00
|
|
|
static void push_message(struct mg_server *server, time_t current_time) {
|
|
|
|
struct mg_connection *c;
|
|
|
|
char buf[20];
|
|
|
|
int len = sprintf(buf, "%lu", (unsigned long) current_time);
|
2013-09-30 05:43:58 +08:00
|
|
|
|
2014-09-09 16:27:35 +08:00
|
|
|
// Iterate over all connections, and push current time message to websocket ones.
|
|
|
|
for (c = mg_next(server, NULL); c != NULL; c = mg_next(server, c)) {
|
|
|
|
if (c->is_websocket) {
|
|
|
|
mg_websocket_write(c, 1, buf, len);
|
|
|
|
}
|
2013-09-28 18:00:54 +08:00
|
|
|
}
|
2014-01-17 18:17:15 +08:00
|
|
|
}
|
2013-09-28 18:00:54 +08:00
|
|
|
|
2014-03-02 20:16:09 +08:00
|
|
|
static int send_reply(struct mg_connection *conn) {
|
2014-01-17 18:17:15 +08:00
|
|
|
if (conn->is_websocket) {
|
2014-01-18 00:37:05 +08:00
|
|
|
// This handler is called for each incoming websocket frame, one or more
|
|
|
|
// times for connection lifetime.
|
|
|
|
// Echo websocket data back to the client.
|
2014-01-17 18:17:15 +08:00
|
|
|
mg_websocket_write(conn, 1, conn->content, conn->content_len);
|
2014-01-28 20:42:13 +08:00
|
|
|
return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ?
|
2014-03-02 20:16:09 +08:00
|
|
|
MG_FALSE : MG_TRUE;
|
2014-01-17 18:17:15 +08:00
|
|
|
} else {
|
2014-10-09 16:26:45 +08:00
|
|
|
mg_send_file(conn, "index.html", NULL);
|
2014-09-09 16:27:35 +08:00
|
|
|
return MG_MORE;
|
2014-03-02 20:16:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
2014-09-09 16:27:35 +08:00
|
|
|
switch (ev) {
|
|
|
|
case MG_AUTH: return MG_TRUE;
|
|
|
|
case MG_REQUEST: return send_reply(conn);
|
|
|
|
default: return MG_FALSE;
|
2014-01-17 18:17:15 +08:00
|
|
|
}
|
2012-09-19 19:31:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2014-03-02 20:16:09 +08:00
|
|
|
struct mg_server *server = mg_create_server(NULL, ev_handler);
|
|
|
|
time_t current_timer = 0, last_timer = time(NULL);
|
2014-01-17 18:17:15 +08:00
|
|
|
|
|
|
|
mg_set_option(server, "listening_port", "8080");
|
|
|
|
|
|
|
|
printf("Started on port %s\n", mg_get_option(server, "listening_port"));
|
|
|
|
for (;;) {
|
2014-03-02 20:16:09 +08:00
|
|
|
mg_poll_server(server, 100);
|
|
|
|
current_timer = time(NULL);
|
2014-01-28 20:42:13 +08:00
|
|
|
if (current_timer - last_timer > 0) {
|
2014-01-17 18:17:15 +08:00
|
|
|
last_timer = current_timer;
|
2014-09-09 16:27:35 +08:00
|
|
|
push_message(server, current_timer);
|
2014-01-17 18:17:15 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 19:31:19 +08:00
|
|
|
|
2014-01-17 18:17:15 +08:00
|
|
|
mg_destroy_server(&server);
|
2012-09-19 19:31:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|