OSDN Git Service

Ver.1.4.9: Change hello timing control from client side to server side.
[opengate/opengate.git] / opengate / javahtml / httpkeep.js
1 //<!--
2 var httpkeepUrl;
3 var appletDescription;
4 var httpObj;
5 var nextKey;
6 var nowKey;
7 var sessionId;
8
9 function createXMLHttpRequest() {
10   try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
11   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
12   try { return new XMLHttpRequest();                   } catch(e) {}
13   return null;
14 }
15
16 function sendHello(){
17
18   // http communication
19   if((httpObj=createXMLHttpRequest())!=null){
20     httpObj.onreadystatechange = displayResponse;
21
22     try{  
23       httpObj.open("GET",httpkeepUrl+"-"+nowKey+"-"+hex_md5(nextKey+sessionId) ,true);
24       httpObj.send(null);
25       nowKey=nextKey;
26       rand=Math.random();
27       nextKey=hex_md5(String(rand));
28     }catch(e){
29       document.getElementById("watchdog").innerHTML = appletDescription;
30     }
31   }
32
33   // java communication
34   else{
35     document.getElementById("watchdog").innerHTML = appletDescription;
36   }
37 }
38
39 function displayResponse()
40 {
41   try{
42     if(httpObj.readyState==4){
43       if(httpObj.status==200){
44         today=new Date; 
45         hh=today.getHours();  mm=today.getMinutes();
46         if(hh<10)hh='0'+hh;   if(mm<10)mm='0'+mm;
47         document.getElementById("dispmsg").innerHTML = hh+':'+mm;
48         if(httpObj.responseText=="hello") sendHello();
49       }else{
50         document.getElementById("dispmsg").innerHTML = "Error!";
51       }
52     }
53   }catch(e){
54     document.getElementById("watchdog").innerHTML = appletDescription;
55   }
56 }
57
58 function initHttpkeep(urlArg, appletArg, sid)
59 {
60   // save arguments
61   httpkeepUrl=urlArg;
62   appletDescription=appletArg;
63   nowKey=hex_md5(sid);
64   sessionId=sid;
65   rand=Math.random();
66   nextKey=hex_md5(String(rand));
67
68   // set timeout and send hello 
69   sendHello();
70 }
71 //-->