Browse Source

Merge pull request #41 from bel2125/master

Finalize the websocket clock example
sunsetbrew 11 years ago
parent
commit
7d03c0f58d
2 changed files with 46 additions and 34 deletions
  1. 26 32
      test/websocket.lua
  2. 20 2
      test/websocket.xhtml

+ 26 - 32
test/websocket.lua

@@ -1,58 +1,52 @@
--- Open database
-local db = sqlite3.open('r:\\websockLog.db')
-
-if db then
-  db:busy_timeout(200);
-  -- Create a table if it is not created already
-  db:exec([[
-    CREATE TABLE IF NOT EXISTS requests (
-      id INTEGER PRIMARY KEY AUTOINCREMENT,
-      timestamp NOT NULL,
-      method NOT NULL,
-      uri NOT NULL,
-      addr
-    );
-  ]])
-end
 
+function iswebsocket()
+  return pcall(function()
+    if (string.upper(mg.request_info.http_headers.Upgrade)~="WEBSOCKET") then error("") end
+  end)
+end
 
-local function logDB(method)
-  -- Add entry about this request
-  local r;
-  repeat
-    r = db:exec([[INSERT INTO requests VALUES(NULL, datetime("now"), "]] .. method .. [[", "]] .. mg.request_info.uri .. [[", "]] .. mg.request_info.remote_port .. [[");]]);
-  until r~=5;
+if not iswebsocket() then
+  mg.write("HTTP/1.0 403 Forbidden\r\n")
+  mg.write("Connection: close\r\n")
+  mg.write("\r\n")
+  return
 end
 
 
 -- Callback for "Websocket ready"
 function ready()
-  logDB("WEBSOCKET READY")
   mg.write("text", "Websocket ready")
 end
 
 -- Callback for "Websocket received data"
 function data(bits, content)
-    logDB(string.format("WEBSOCKET DATA (%x)", bits))
-    mg.write("text", os.date())
-    return true;
 end
 
 -- Callback for "Websocket is closing"
 function close()
-  logDB("WEBSOCKET CLOSE")
-  -- Close database
-  db:close()
 end
 
 
--- Websocket with coroutines
-logDB("WEBSOCKET PREPARE")
-
 coroutine.yield(true); -- first yield returns (true) or (false) to accept or reject the connection
+
 ready()
+
+local lasthand = ""
+
 repeat
     local cont, bits, content = coroutine.yield(true, 1.0)
+
+    local date = os.date('*t');
+    local hand = (date.hour%12)*60+date.min;
+
+    mg.write("text", string.format("%u:%02u:%02u", date.hour, date.min, date.sec));
+
+    if (hand ~= lasthand) then
+        mg.write("text", string.format("-->h %u", hand*360/(12*60)));
+        mg.write("text", string.format("-->m %u", date.min*360/60));
+        lasthand = hand;
+    end
+
     if bits and content then
         data(bits, content)
     end

+ 20 - 2
test/websocket.xhtml

@@ -17,6 +17,8 @@
 
     var connection;
     var websock_text_field;
+    var hand_hour;
+    var hand_min;
 
     function webSockKeepAlive() {
       if (keepAlive) {
@@ -28,6 +30,8 @@
     function load() {
       connection = new WebSocket("ws://" + window.location.host + "/websocket.lua");
       websock_text_field = document.getElementById('websock_text_field');
+      hand_min = document.getElementById('hand_min');
+      hand_hour = document.getElementById('hand_hour');
 
       connection.onopen = function () {
         keepAlive = true;
@@ -42,8 +46,20 @@
       };
 
       // Log messages from the server
-      connection.onmessage = function (e) {        
-        websock_text_field.textContent = e.data;
+      connection.onmessage = function (e) {
+        var lCmd = e.data.substring(0,3);
+        if (lCmd == "-->") {
+          //console.log(e.data);
+          var lDirection = Number(e.data.substring(5));
+          if (e.data[3] == 'h') {
+            hand_hour.setAttribute("transform", "rotate(" + lDirection + " 800 600)");
+          }
+          if (e.data[3] == 'm') {
+            hand_min.setAttribute("transform", "rotate(" + lDirection + " 800 600)");
+          }
+        } else {
+          websock_text_field.textContent = e.data;
+        }
       };
     }
 
@@ -59,6 +75,8 @@
   >
 
   <circle id="line_a" cx="800" cy="600" r="500" style="stroke:rgb(255,0,0); stroke-width:5; fill:rgb(200,200,200)"/>
+  <polygon points="800,200 900,300 850,300 850,600 750,600 750,300 700,300" style="fill:rgb(100,0,0)" transform="rotate(0,800,600)" id="hand_hour"/>
+  <polygon points="800,100 840,200 820,200 820,600 780,600 780,200 760,200" style="fill:rgb(0,100,0)" transform="rotate(0,800,600)" id="hand_min"/>
   <text id="websock_text_field" x="800" y="600" text-anchor="middle" font-size="50px" fill="red">No websocket connection yet</text>
 
 </svg>