OSDN Git Service

グラフ自動追尾機能を追加。
[chnosproject/AI004.git] / ainet.js
1 function AI_NetworkManager(env){
2         this.env = env;
3         this.PHPExtPath = "./ainet.php";
4 }
5 AI_NetworkManager.prototype = {
6         //from PCD2013GSCL
7         //https://sourceforge.jp/projects/h58pcdgame/scm/git/GameScriptCoreLibrary/blobs/master/www/corelib/coresubc.js
8         //http://hakuhin.jp/js/xmlhttprequest.html
9         CreateRequestObject: function(){
10                 var rq = null;
11                 // XMLHttpRequest
12                 try{
13                         // XMLHttpRequest オブジェクトを作成
14                         rq = new XMLHttpRequest();
15                 }catch(e){}
16                 // Internet Explorer
17                 try{
18                         rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
19                 }catch(e){}
20                 try{
21                         rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
22                 }catch(e){}
23                 try{
24                         rq = new ActiveXObject('MSXML2.XMLHTTP');
25                 }catch(e){}
26                 if(rq == null){
27                         return null;
28                 }
29                 return rq;
30         },
31         RequestObjectDisableCache: function(rq){
32                 //call after open request.
33                 //disable cache
34                 //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
35                 rq.setRequestHeader('Pragma', 'no-cache');                              // HTTP/1.0 における汎用のヘッダフィールド
36                 rq.setRequestHeader('Cache-Control', 'no-cache');               // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
37                 rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
38                 
39         },
40         sendRequestSync: function(mode, url, data){
41                 //同期モード
42                 var q = this.CreateRequestObject();
43                 q.open(mode, url, false);
44                 q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
45                 this.RequestObjectDisableCache(q);
46                 try {
47                         q.send(data);
48                 } catch(e){
49                         this.env.debug("AI_NetworkManager:sendRequestSync:Network Error.\n");
50                         return null;
51                 }
52                 if(q.status == 0){
53                         alert("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
54                 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
55                         var res = q.responseText;
56                         return res;
57                 }else{
58                         alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
59                 }
60                 return null;
61         },
62         sendRequestAsync: function(mode, url, data, callback){
63                 //非同期モード
64                 //callback(res);
65                 var q = this.CreateRequestObject();
66                 var that = this;
67                 q.onreadystatechange = function(){
68                         if(q.readyState == 4){
69                                 if(q.status == 0){
70                                         alert("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
71                                 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
72                                         var res = q.responseText;
73                                         callback(res);
74                                 }else{
75                                         alert("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
76                                 }
77                         }
78                 };
79                 q.open(mode, url, true);
80                 q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
81                 this.RequestObjectDisableCache(q);
82                 q.send(data);
83         },
84         sendRequestThroughPHPSync: function(mode, url, data){
85                 var sendURL = this.PHPExtPath;
86                 sendURL += "?cmd=httpreq&url=";
87                 sendURL += encodeURIComponent(url);
88                 return this.sendRequestSync("GET", sendURL);
89         },
90 }