diff --git a/jstrr.js b/jstrr.js index 2f28a14..9b5682e 100644 --- a/jstrr.js +++ b/jstrr.js @@ -518,8 +518,6 @@ }, 1000); } } else if (data.scoreinfo) { - //console.log("DSSSS="+JSON.stringify(data.scoreinfo)); - //console.log("STRI="+JSON.stringify(data.scoreinfo.steptrial)); mystate.scoreinfo = data.scoreinfo; updateScoreField(); } else if (data.ranking) { @@ -528,6 +526,12 @@ } else if (data.yourtext) { console.log("YT="+data.yourtext); loadTextToElement1(data.yourtext); + } else if (data.css) { + let csslink = document.createElement("link"); + csslink.setAttribute("rel", "stylesheet"); + csslink.setAttribute("href", data.css); + console.log(csslink); + document.querySelector("head").appendChild(csslink); } else { console.log("message: "+data.message); } diff --git a/jtserv/jtserv.rb b/jtserv/jtserv.rb index bf33d29..6ff0013 100755 --- a/jtserv/jtserv.rb +++ b/jtserv/jtserv.rb @@ -5,8 +5,10 @@ require 'csv' require 'sqlite3' -PORT = (ENV["JTSV_PORT"]||8814).to_i -WARNMAX = 3 +PORT = (ENV["JTSV_PORT"]||8814).to_i +WARNMAX = 3 +LOCALCSS = ENV["JTSV_LOCALCSS"]||"local.css" +TIMEOUT = (ENV["JTSV_TIMEOUT"]||60*5).to_i # Interchange of Information is done in JSON form. # (IN) {"cmd": Command, OtherArgs...} @@ -439,8 +441,19 @@ end } ws_conn.onopen do - connections[ws_conn] = {} + # First, clean up idling connections + connections.each do |conn, info| + if TIMEOUT < Time.now - (info["stamp"]||info["openat"]) + STDERR.printf("Force Close client %s after long idle\n", conn) + conn.close + end + end + # Start to accept a new client + connections[ws_conn] = {"openat": Time.now} ws_conn.send(JSON.generate({myid: connections.length})) + if test(?e, File.expand_path("..", LOCALCSS)) + ws_conn.send(JSON.generate({css: LOCALCSS})) + end STDERR.printf("OP: %d clients [%s]\n", connections.length, connections.values.collect{|c| c["user"]}.join(", ")) @@ -449,16 +462,20 @@ connections.delete(ws_conn) STDERR.printf("CL: %d clients\n", connections.length) end + ## + ## Main Loop + ## ws_conn.onmessage do |msg| begin json = JSON.parse(msg) rescue - return + next ## Skip client that sends invalid JSON end userinfo = connections[ws_conn] user, skey = userinfo['user'], userinfo['skey'] STDERR.printf("RECV%s: [%s]\n", user ? "("+user+")" : "", json.reject{|k,v|k=="Passcode"}.to_json) + userinfo["stamp"] = Time.now # Remember time stamp if skey then userinfo["trial"] = 0 # Reset auth count if json["team"] then