OSDN Git Service

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