OSDN Git Service

ファーストコミット:同期用コントローラスクリプトをアップロード master
authornandenjin <timer.ground.gm@gmail.com>
Fri, 11 Jul 2014 23:48:12 +0000 (08:48 +0900)
committernandenjin <timer.ground.gm@gmail.com>
Fri, 11 Jul 2014 23:48:12 +0000 (08:48 +0900)
commands.json [new file with mode: 0644]
index.html [new file with mode: 0644]
plugin.js [new file with mode: 0644]
receiver.php [new file with mode: 0644]
server.php [new file with mode: 0644]

diff --git a/commands.json b/commands.json
new file mode 100644 (file)
index 0000000..40d5d48
--- /dev/null
@@ -0,0 +1 @@
+[{"id":"dummy","time":0,"action":"none","data":""}]
\ No newline at end of file
diff --git a/index.html b/index.html
new file mode 100644 (file)
index 0000000..f919fe0
--- /dev/null
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html>
+       <head>
+               <meta name='robots' content='noindex'>
+               <meta charset='utf-8'>
+               <title>Controller</title>
+               <style>
+                       body{
+                               color:#FFF;
+                               background:#333;
+                       }
+                       .btn{
+                               display:block;
+                               width:90%;
+                               background:#555;
+                               font-size:20px;
+                               margin:10px;
+                               padding:15px;
+                               border:none;
+                       }
+               </style>
+               <script>
+                       function send(action,data){
+                               var req=new XMLHttpRequest();
+                               req.open('POST','receiver.php',true);
+                               req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
+                               var id="";
+                               for(var i=0;i<5;i++){
+                                       id+="abcdefghijklmnopqrstuvwxyz".charAt(Math.floor(Math.random()*26));
+                               }
+                               req.onload=function(){
+                                       document.getElementById('msg').innerHTML=req.response;
+                                       setTimeout(function(){
+                                               document.getElementById('msg').innerHTML="";
+                                       },2000);
+                               };
+                               req.onerror=function(){
+                                       document.getElementById('msg').innerHTML="CONNECT ERROR";
+                                       setTimeout(function(){
+                                               document.getElementById('msg').innerHTML="";
+                                       },2000);
+                               };
+                               req.send('cmd='+JSON.stringify({
+                                       action:action,
+                                       data:data,
+                                       id:id
+                               }));
+                       }
+               </script>
+       </head>
+       <body>
+               <input type='button' value='reload' class="btn" onclick="send('reload','')">
+               <input type='button' value='jump' class="btn" onclick="send('jump',prompt('ジャンプ先の選択',''))">
+               <div id="msg"></div>
+       </body>
+</html>
\ No newline at end of file
diff --git a/plugin.js b/plugin.js
new file mode 100644 (file)
index 0000000..5017c29
--- /dev/null
+++ b/plugin.js
@@ -0,0 +1,23 @@
+//プログラム リモート コントローラー
+//プログラムの作成中に複数台の端末を一斉に制御し、更新やページ移動を簡単にできるようにしています。
+//plugin.js - 実際に制御されるプログラムに埋め込んでおきます。他のスクリプトには影響を与えません。
+window.addEventListener('load',function(){
+       //イベントストリームでserver.phpに接続
+       var stream=new EventSource('http://'+location.hostname+'/control/server.php');
+       //再読み込み
+       stream.addEventListener('reload',function(e){
+               if(localStorage.sshCtrlCmdId==JSON.parse(e.data).id){
+                       return false;
+               }
+               localStorage.sshCtrlCmdId=JSON.parse(e.data).id;
+               location.reload();
+       });
+       //ページジャンプ
+       stream.addEventListener('jump',function(e){
+               if(localStorage.sshCtrlCmdId==JSON.parse(e.data).id){
+                       return false;
+               }
+               localStorage.sshCtrlCmdId=JSON.parse(e.data).id;
+               location.href=JSON.parse(e.data).data;
+       });
+});
\ No newline at end of file
diff --git a/receiver.php b/receiver.php
new file mode 100644 (file)
index 0000000..b81f7c4
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+$d=json_decode($_POST['cmd'],true);
+$d['time']=time();
+$f=json_decode(file_get_contents('commands.json'));
+$f[]=$d;
+file_put_contents('commands.json',json_encode($f));
+echo 'SUCCESS!YOUR COMMAND WAS SENDED';
+?>
\ No newline at end of file
diff --git a/server.php b/server.php
new file mode 100644 (file)
index 0000000..847f9f1
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+//プログラム リモート コントローラー
+//プログラムの作成中に複数台の端末を一斉に制御し、更新やページ移動を簡単にできるようにしています。
+//server.php - 遠隔操作の指令を配信するサーバーサイドプログラムです。
+header("Content-Type:text/event-stream\n\n");
+$last_id='dummy';
+while(1){
+       echo ":\n\n";
+       $p=json_decode(file_get_contents('commands.json'),true);
+       $current_cmd=$p[count($p)-1];
+       if($current_cmd['id']!=$last_id&&$current_cmd['time']>=time()-30){
+               echo 'event:'.$current_cmd['action']."\n";
+               echo 'data:'.json_encode($current_cmd)."\n\n";
+               $last_id=$current_cmd['id'];
+       }else if($current_cmd['id']!='dummy'&&$current_cmd['time']<time()-30){
+               array_pop($p);
+               file_put_contents('commands.json',json_encode($p));
+       }
+       ob_flush();
+       flush();
+       sleep(1);
+}
+?>
\ No newline at end of file