|  | @@ -15,7 +15,6 @@ struct ws_connection {
 | 
											
												
													
														|  |      struct mg_connection    *conn;
 |  |      struct mg_connection    *conn;
 | 
											
												
													
														|  |      int     update;
 |  |      int     update;
 | 
											
												
													
														|  |      int     closing;
 |  |      int     closing;
 | 
											
												
													
														|  | -    long    counter;
 |  | 
 | 
											
												
													
														|  |  };
 |  |  };
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  // time base and structure periodic updates to client for demo
 |  |  // time base and structure periodic updates to client for demo
 | 
											
										
											
												
													
														|  | @@ -31,28 +30,6 @@ struct progress {
 | 
											
												
													
														|  |  #define CONNECTIONS 16
 |  |  #define CONNECTIONS 16
 | 
											
												
													
														|  |  static struct ws_connection ws_conn[CONNECTIONS];
 |  |  static struct ws_connection ws_conn[CONNECTIONS];
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -#define PING_ACTIVE
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -#define PING_THREAD
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -#if defined(PING_ACTIVE) && defined(PING_THREAD)
 |  | 
 | 
											
												
													
														|  | -// ws_ping_thread()
 |  | 
 | 
											
												
													
														|  | -// Send periodic PING to assure websocket remains connected, except if we are closing
 |  | 
 | 
											
												
													
														|  | -static void *ws_ping_thread(void *parm)
 |  | 
 | 
											
												
													
														|  | -{
 |  | 
 | 
											
												
													
														|  | -    int wsd = (long)parm;
 |  | 
 | 
											
												
													
														|  | -    struct mg_connection    *conn = ws_conn[wsd].conn;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -    while(!ws_conn[wsd].closing)
 |  | 
 | 
											
												
													
														|  | -    {
 |  | 
 | 
											
												
													
														|  | -        usleep(8000);   /* 8 ms */
 |  | 
 | 
											
												
													
														|  | -        if (!ws_conn[wsd].closing)
 |  | 
 | 
											
												
													
														|  | -            mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0);
 |  | 
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  | -    fprintf(stderr, "ws_ping_thread %d exiting\n", wsd);
 |  | 
 | 
											
												
													
														|  | -}
 |  | 
 | 
											
												
													
														|  | -#endif
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  // ws_server_thread()
 |  |  // ws_server_thread()
 | 
											
												
													
														|  |  // Simple demo server thread. Sends periodic updates to connected clients
 |  |  // Simple demo server thread. Sends periodic updates to connected clients
 | 
											
										
											
												
													
														|  | @@ -87,13 +64,9 @@ static void *ws_server_thread(void *parm)
 | 
											
												
													
														|  |          mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
 |  |          mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr));
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -#if defined(PING_ACTIVE) && defined(PING_THREAD)
 |  | 
 | 
											
												
													
														|  | -    mg_start_thread(ws_ping_thread, (void *)(long)wsd);
 |  | 
 | 
											
												
													
														|  | -#endif
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |      /* While the connection is open, send periodic updates */
 |  |      /* While the connection is open, send periodic updates */
 | 
											
												
													
														|  |      while(!ws_conn[wsd].closing) {
 |  |      while(!ws_conn[wsd].closing) {
 | 
											
												
													
														|  | -        usleep(10000); /* 0.01 second */
 |  | 
 | 
											
												
													
														|  | 
 |  | +        usleep(100000); /* 0.1 second */
 | 
											
												
													
														|  |          timer++;
 |  |          timer++;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |          /* Send meter updates */
 |  |          /* Send meter updates */
 | 
											
										
											
												
													
														|  | @@ -116,11 +89,9 @@ static void *ws_server_thread(void *parm)
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -#if defined(PING_ACTIVE) && !defined(PING_THREAD)
 |  | 
 | 
											
												
													
														|  |          /* Send periodic PING to assure websocket remains connected, except if we are closing */
 |  |          /* Send periodic PING to assure websocket remains connected, except if we are closing */
 | 
											
												
													
														|  |          if (timer%100 == 0 && !ws_conn[wsd].closing)
 |  |          if (timer%100 == 0 && !ws_conn[wsd].closing)
 | 
											
												
													
														|  |              mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0);
 |  |              mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0);
 | 
											
												
													
														|  | -#endif
 |  | 
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      fprintf(stderr, "ws_server_thread %d exiting\n", wsd);
 |  |      fprintf(stderr, "ws_server_thread %d exiting\n", wsd);
 | 
											
										
											
												
													
														|  | @@ -149,7 +120,6 @@ static int websocket_connect_handler(const struct mg_connection *conn)
 | 
											
												
													
														|  |              ws_conn[i].conn = (struct mg_connection *)conn;
 |  |              ws_conn[i].conn = (struct mg_connection *)conn;
 | 
											
												
													
														|  |              ws_conn[i].closing = 0;
 |  |              ws_conn[i].closing = 0;
 | 
											
												
													
														|  |              ws_conn[i].update = 0;
 |  |              ws_conn[i].update = 0;
 | 
											
												
													
														|  | -            ws_conn[i].counter = -1;
 |  | 
 | 
											
												
													
														|  |              break;
 |  |              break;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
										
											
												
													
														|  | @@ -222,40 +192,19 @@ static int websocket_data_handler(struct mg_connection *conn, int flags,
 | 
											
												
													
														|  |              fprintf(stderr, "CONTINUATION...\n");
 |  |              fprintf(stderr, "CONTINUATION...\n");
 | 
											
												
													
														|  |              break;
 |  |              break;
 | 
											
												
													
														|  |          case WEBSOCKET_OPCODE_TEXT:
 |  |          case WEBSOCKET_OPCODE_TEXT:
 | 
											
												
													
														|  | -            //fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
 |  | 
 | 
											
												
													
														|  | 
 |  | +            fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
 | 
											
												
													
														|  |              /*** interpret data as commands here ***/
 |  |              /*** interpret data as commands here ***/
 | 
											
												
													
														|  |              if (strncmp("update on", data, data_len)== 0) {
 |  |              if (strncmp("update on", data, data_len)== 0) {
 | 
											
												
													
														|  | -                fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
 |  | 
 | 
											
												
													
														|  |                  /* turn on updates */
 |  |                  /* turn on updates */
 | 
											
												
													
														|  |                  ws_conn[wsd].update = 1;
 |  |                  ws_conn[wsd].update = 1;
 | 
											
												
													
														|  |                  /* echo back */
 |  |                  /* echo back */
 | 
											
												
													
														|  |                  mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
 |  |                  mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
 | 
											
												
													
														|  |              } else if (strncmp("update off", data, data_len)== 0) {
 |  |              } else if (strncmp("update off", data, data_len)== 0) {
 | 
											
												
													
														|  | -                fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
 |  | 
 | 
											
												
													
														|  |                  /* turn off updates */
 |  |                  /* turn off updates */
 | 
											
												
													
														|  |                  ws_conn[wsd].update = 0;
 |  |                  ws_conn[wsd].update = 0;
 | 
											
												
													
														|  |                  /* echo back */
 |  |                  /* echo back */
 | 
											
												
													
														|  |                  mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
 |  |                  mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
 | 
											
												
													
														|  | -            } else if (strncmp("counter ", data, 8)== 0) {
 |  | 
 | 
											
												
													
														|  | -                char buffer[16];
 |  | 
 | 
											
												
													
														|  | -                long newval;
 |  | 
 | 
											
												
													
														|  | -                strncpy(buffer, &data[8], data_len-8);
 |  | 
 | 
											
												
													
														|  | -                buffer[data_len-8] = '\0';
 |  | 
 | 
											
												
													
														|  | -                newval = strtol(buffer, NULL, 0);
 |  | 
 | 
											
												
													
														|  | -                if (ws_conn[wsd].counter == -1)
 |  | 
 | 
											
												
													
														|  | -                    ws_conn[wsd].counter = newval;
 |  | 
 | 
											
												
													
														|  | -                else
 |  | 
 | 
											
												
													
														|  | -                    ws_conn[wsd].counter++;
 |  | 
 | 
											
												
													
														|  | -                if (ws_conn[wsd].counter != newval)
 |  | 
 | 
											
												
													
														|  | -                {
 |  | 
 | 
											
												
													
														|  | -                    fprintf(stderr, "Counter: %ld, received %ld\n", ws_conn[wsd].counter, newval);
 |  | 
 | 
											
												
													
														|  | -                    ws_conn[wsd].counter = newval;
 |  | 
 | 
											
												
													
														|  | -                }
 |  | 
 | 
											
												
													
														|  | -                if (ws_conn[wsd].counter % 3000 == 0)
 |  | 
 | 
											
												
													
														|  | -                    fprintf(stderr, "Counter: %ld\n", ws_conn[wsd].counter);
 |  | 
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  | -            else
 |  | 
 | 
											
												
													
														|  | -                fprintf(stderr, "TEXT: %-.*s\n", (int)data_len, data);
 |  | 
 | 
											
												
													
														|  |              break;
 |  |              break;
 | 
											
												
													
														|  |          case WEBSOCKET_OPCODE_BINARY:
 |  |          case WEBSOCKET_OPCODE_BINARY:
 | 
											
												
													
														|  |              fprintf(stderr, "BINARY...\n");
 |  |              fprintf(stderr, "BINARY...\n");
 |