2015-09-08 19:49:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Cesanta Software Limited
|
|
|
|
* All rights reserved
|
|
|
|
*/
|
2014-09-09 06:30:52 +08:00
|
|
|
|
2014-06-10 00:25:08 +08:00
|
|
|
#include "mongoose.h"
|
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
static sig_atomic_t s_signal_received = 0;
|
|
|
|
static const char *s_http_port = "8000";
|
2014-06-10 00:25:08 +08:00
|
|
|
|
|
|
|
static void signal_handler(int sig_num) {
|
|
|
|
signal(sig_num, signal_handler); // Reinstantiate signal handler
|
|
|
|
s_signal_received = sig_num;
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
static int is_websocket(const struct mg_connection *nc) {
|
2015-09-19 19:38:46 +08:00
|
|
|
return nc->flags & MG_F_IS_WEBSOCKET;
|
2015-09-08 19:49:03 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 00:47:44 +08:00
|
|
|
static void broadcast(struct mg_connection *nc, const struct mg_str msg) {
|
2014-06-10 00:25:08 +08:00
|
|
|
struct mg_connection *c;
|
2015-09-08 19:49:03 +08:00
|
|
|
char buf[500];
|
2016-04-28 00:47:44 +08:00
|
|
|
char addr[32];
|
|
|
|
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
|
|
|
|
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
|
2014-06-10 00:25:08 +08:00
|
|
|
|
2016-04-28 00:47:44 +08:00
|
|
|
snprintf(buf, sizeof(buf), "%s %.*s", addr, (int) msg.len, msg.p);
|
|
|
|
printf("%s\n", buf); /* Local echo. */
|
2015-09-08 19:49:03 +08:00
|
|
|
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
|
2016-04-28 00:47:44 +08:00
|
|
|
if (c == nc) continue; /* Don't send to the sender. */
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, strlen(buf));
|
2014-06-10 00:25:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
2014-06-10 00:25:08 +08:00
|
|
|
switch (ev) {
|
2016-04-28 00:47:44 +08:00
|
|
|
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
|
2015-09-08 19:49:03 +08:00
|
|
|
/* New websocket connection. Tell everybody. */
|
2016-04-28 00:47:44 +08:00
|
|
|
broadcast(nc, mg_mk_str("++ joined"));
|
2015-09-08 19:49:03 +08:00
|
|
|
break;
|
2016-04-28 00:47:44 +08:00
|
|
|
}
|
|
|
|
case MG_EV_WEBSOCKET_FRAME: {
|
|
|
|
struct websocket_message *wm = (struct websocket_message *) ev_data;
|
2015-09-08 19:49:03 +08:00
|
|
|
/* New websocket message. Tell everybody. */
|
2016-04-28 00:47:44 +08:00
|
|
|
struct mg_str d = {(char *) wm->data, wm->size};
|
|
|
|
broadcast(nc, d);
|
2015-09-08 19:49:03 +08:00
|
|
|
break;
|
2016-04-28 00:47:44 +08:00
|
|
|
}
|
|
|
|
case MG_EV_CLOSE: {
|
2015-09-08 19:49:03 +08:00
|
|
|
/* Disconnect. Tell everybody. */
|
|
|
|
if (is_websocket(nc)) {
|
2016-04-28 00:47:44 +08:00
|
|
|
broadcast(nc, mg_mk_str("-- left"));
|
2014-06-10 00:25:08 +08:00
|
|
|
}
|
2015-09-08 19:49:03 +08:00
|
|
|
break;
|
2016-04-28 00:47:44 +08:00
|
|
|
}
|
2014-06-10 00:25:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2015-09-08 19:49:03 +08:00
|
|
|
struct mg_mgr mgr;
|
|
|
|
struct mg_connection *nc;
|
2014-09-09 06:29:34 +08:00
|
|
|
|
2014-06-10 00:25:08 +08:00
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGINT, signal_handler);
|
2016-04-28 00:47:44 +08:00
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
setvbuf(stderr, NULL, _IOLBF, 0);
|
2014-09-09 06:29:34 +08:00
|
|
|
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_mgr_init(&mgr, NULL);
|
|
|
|
|
|
|
|
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
|
|
mg_set_protocol_http_websocket(nc);
|
|
|
|
|
|
|
|
printf("Started on port %s\n", s_http_port);
|
2014-06-10 00:25:08 +08:00
|
|
|
while (s_signal_received == 0) {
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_mgr_poll(&mgr, 200);
|
2014-06-10 00:25:08 +08:00
|
|
|
}
|
2015-09-08 19:49:03 +08:00
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
|
2014-06-10 00:25:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|