OSDN Git Service

ファーストコミット:同期用コントローラスクリプトをアップロード
[digital-art/control.git] / server.php
1 <?php
2 //プログラム リモート コントローラー
3 //プログラムの作成中に複数台の端末を一斉に制御し、更新やページ移動を簡単にできるようにしています。
4 //server.php - 遠隔操作の指令を配信するサーバーサイドプログラムです。
5 header("Content-Type:text/event-stream\n\n");
6 $last_id='dummy';
7 while(1){
8         echo ":\n\n";
9         $p=json_decode(file_get_contents('commands.json'),true);
10         $current_cmd=$p[count($p)-1];
11         if($current_cmd['id']!=$last_id&&$current_cmd['time']>=time()-30){
12                 echo 'event:'.$current_cmd['action']."\n";
13                 echo 'data:'.json_encode($current_cmd)."\n\n";
14                 $last_id=$current_cmd['id'];
15         }else if($current_cmd['id']!='dummy'&&$current_cmd['time']<time()-30){
16                 array_pop($p);
17                 file_put_contents('commands.json',json_encode($p));
18         }
19         ob_flush();
20         flush();
21         sleep(1);
22 }
23 ?>