Browse Source

changed ajax to get json and integrate a javascript countdown.

master
Georg Hopp 14 years ago
parent
commit
48c8b070fc
  1. 37
      src/http/response/me.c
  2. 17
      src/http/response/randval.c

37
src/http/response/me.c

@ -45,7 +45,6 @@
"left: 200px;" \
"top: 100px;" \
"position: fixed;" \
"overflow: none;" \
"background-color: white;" \
"border: 1px solid black;" \
"}" \
@ -59,23 +58,53 @@
"<script type=\"text/javascript\" src=\"/jquery/\"></script>" \
"<script>" \
"$(document).ready(function() {" \
"var intervalId;" \
"var vnext = 0;" \
"var counter = function() {" \
"if (0 >= vnext) {" \
"$.getJSON(\"/randval/\", function(data) {" \
"var date = new Date(data.ctime * 1000);" \
"$(\"#ctime\").empty().append(date.toString());" \
"vnext = data.vnext;" \
"$(\"#value\").empty().append(data.value);" \
"});" \
"} else {" \
"vnext--;" \
"}" \
"$(\"#vnext\").empty().append(vnext);" \
"};" \
"$(\"#msg\").ajaxError(function(event, request, settings) {" \
"$(this).append(" \
"\"<li>Error requesting page \" + " \
"settings.url + " \
"\"</li>\");" \
"});" \
"$(\"a\").click(function() {" \
"$(\"#randval\").load(\"/randval/\");" \
"intervalId = setInterval(counter, 1000);" \
"$(\"#randval\").removeClass(\"hide\");" \
"});" \
"$(\"#randval\").click(function() {" \
"clearInterval(intervalId);" \
"vnext = 0;" \
"$(\"#randval\").addClass(\"hide\");" \
"});" \
"});" \
"</script>" \
"</head>" \
"<body>" \
"<div id=\"randval\" class=\"hide\"></div>" \
"<div id=\"randval\" class=\"hide\">" \
"<span class=\"small\">" \
"Value created at: <br /><span id=\"ctime\"></span><br>" \
"Next value in: <span id=\"vnext\"></span><br />" \
"</span>" \
"Value: <span id=\"value\"></span>" \
"</div>" \
"<div id=\"main\">" \
"<h1>Testpage</h1>" \
"<img src=\"/image/\" />" \
"<hr /><a href=\"#\">Link</a>" \
"<br /><a href=\"#\">Link</a>" \
"</div>" \
"<hr /><div id=\"msg\"></div>" \
"</body>" \
"</html>"

17
src/http/response/randval.c

@ -33,9 +33,10 @@
#include "http/message.h"
#include "http/header.h"
#define RESP_DATA "<span class=\"small\">" \
"Value created at:<br/>%s<br/>Next value in: %ld seconds</span>" \
"<br />Value: %02d"
//#define RESP_DATA "<span class=\"small\">" \
// "Value created at:<br/>%s<br/>Next value in: %ld seconds</span>" \
// "<br />Value: %02d"
#define RESP_DATA "{\"ctime\":%ld,\"vnext\":%ld,\"value\":%02d}"
HttpResponse
httpResponseRandval(time_t ctime, int value)
@ -45,7 +46,6 @@ httpResponseRandval(time_t ctime, int value)
HttpResponse response;
HttpMessage message;
size_t nbuf;
struct tm * tmp;
time_t remaining;
response = new(HttpResponse, "HTTP/1.1", 200, "OK");
@ -55,17 +55,14 @@ httpResponseRandval(time_t ctime, int value)
new(HttpHeader,
"Content-Type",
sizeof("Content-Type")-1,
"text/html",
sizeof("text/html")-1));
"application/json",
sizeof("application/json")-1));
message->type = HTTP_MESSAGE_BUFFERED;
tmp = localtime(&ctime);
nbuf = strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %T %Z", tmp);
remaining = 30 - (time(NULL) - ctime);
nbuf = sprintf(buffer, RESP_DATA, timebuf, remaining, value);
nbuf = sprintf(buffer, RESP_DATA, ctime, remaining, value);
message->nbody = nbuf;
message->body = malloc(nbuf);

Loading…
Cancel
Save