websocket_chat.c
Go to the documentation of this file.
00001 // Copyright (c) 2013-2014 Cesanta Software Limited
00002 // $Date: 2014-09-09 17:07:55 UTC $
00003 
00004 #include <string.h>
00005 #include <time.h>
00006 #include <signal.h>
00007 #include <stdlib.h>
00008 #include "mongoose.h"
00009 
00010 static int s_signal_received = 0;
00011 static struct mg_server *s_server = NULL;
00012 
00013 // Data associated with each websocket connection
00014 struct conn_data {
00015   int room;
00016 };
00017 
00018 static void signal_handler(int sig_num) {
00019   signal(sig_num, signal_handler);  // Reinstantiate signal handler
00020   s_signal_received = sig_num;
00021 }
00022 
00023 static void handle_websocket_message(struct mg_connection *conn) {
00024   struct conn_data *d = (struct conn_data *) conn->connection_param;
00025   struct mg_connection *c;
00026 
00027   printf("[%.*s]\n", (int) conn->content_len, conn->content);
00028   if (conn->content_len > 5 && !memcmp(conn->content, "join ", 5)) {
00029     // Client joined new room
00030     d->room = conn->content[5];
00031   } else if (conn->content_len > 4 && !memcmp(conn->content, "msg ", 4) &&
00032              d->room != 0 && d->room != '?') {
00033     // Client has sent a message. Push this message to all clients
00034     // that are subscribed to the same room as client
00035     for (c = mg_next(s_server, NULL); c != NULL; c = mg_next(s_server, c)) {
00036       struct conn_data *d2 = (struct conn_data *) c->connection_param;
00037       if (!c->is_websocket || d2->room != d->room) continue;
00038       mg_websocket_printf(c, WEBSOCKET_OPCODE_TEXT, "msg %c %p %.*s",
00039                           (char) d->room, conn,
00040                           conn->content_len - 4, conn->content + 4);
00041     }
00042   }
00043 }
00044 
00045 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
00046   switch (ev) {
00047     case MG_REQUEST:
00048       if (conn->is_websocket) {
00049         handle_websocket_message(conn);
00050         return MG_TRUE;
00051       } else {
00052         mg_send_file(conn, "index.html", NULL);  // Return MG_MORE after!
00053         return MG_MORE;
00054       }
00055     case MG_WS_CONNECT:
00056       // New websocket connection. Send connection ID back to the client.
00057       conn->connection_param = calloc(1, sizeof(struct conn_data));
00058       mg_websocket_printf(conn, WEBSOCKET_OPCODE_TEXT, "id %p", conn);
00059       return MG_FALSE;
00060     case MG_CLOSE:
00061       free(conn->connection_param);
00062       return MG_TRUE;
00063     case MG_AUTH:
00064       return MG_TRUE;
00065     default:
00066       return MG_FALSE;
00067   }
00068 }
00069 
00070 int main(void) {
00071   s_server = mg_create_server(NULL, ev_handler);
00072   mg_set_option(s_server, "listening_port", "8080");
00073 
00074   signal(SIGTERM, signal_handler);
00075   signal(SIGINT, signal_handler);
00076 
00077   printf("Started on port %s\n", mg_get_option(s_server, "listening_port"));
00078   while (s_signal_received == 0) {
00079     mg_poll_server(s_server, 100);
00080   }
00081   mg_destroy_server(&s_server);
00082   return 0;
00083 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07