1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-09-03 01:21:16 +03:00

Restore original test code

This commit is contained in:
William Greathouse
2013-10-13 13:53:13 -04:00
parent 52d2ddbd47
commit 76bebde90d
2 changed files with 2 additions and 68 deletions

View File

@@ -223,7 +223,6 @@
<script language="javascript" type="text/javascript"> <script language="javascript" type="text/javascript">
var connection; // websocket connection var connection; // websocket connection
var count=1;
function writeToScreen (message) { function writeToScreen (message) {
var div = document.createElement('div'); var div = document.createElement('div');
@@ -233,14 +232,6 @@
output.scrollTop = output.scrollHeight; output.scrollTop = output.scrollHeight;
} }
function send_counter() {
if (connection.bufferedAmount == 0)
{
connection.send('counter ' + count);
count = count+1;
}
}
function ws_connect() { function ws_connect() {
// check for websocket support // check for websocket support
// for Internet Explorer < 10 there are options for websocket support that // for Internet Explorer < 10 there are options for websocket support that
@@ -261,7 +252,6 @@
}; };
connection.onclose = function(ev) { connection.onclose = function(ev) {
clearInterval(counterid);
document.getElementById("update").disabled=true; document.getElementById("update").disabled=true;
document.getElementById("update").innerHTML = "Enable Update"; document.getElementById("update").innerHTML = "Enable Update";
document.getElementById("connection").innerHTML = "WebSocket Connect"; document.getElementById("connection").innerHTML = "WebSocket Connect";
@@ -278,18 +268,13 @@
meter.style.width = percent+"%"; meter.style.width = percent+"%";
} }
else else
{
writeToScreen('RECEIVED: ' + ev.data); writeToScreen('RECEIVED: ' + ev.data);
}
send_counter();
}; };
connection.onerror = function(ev) { connection.onerror = function(ev) {
alert("WebSocket error"); alert("WebSocket error");
}; };
counterid = setInterval(send_counter, 10);
} else { } else {
alert("WebSocket is not available!!!\n" + alert("WebSocket is not available!!!\n" +
"Demo will not function."); "Demo will not function.");

View File

@@ -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");