ajaxでクライアントをリモートコントロールするサンプル

仕事で徹夜してしまって脳が沸いとる。
なんか書かないと家まで持たない。
電車内で書きました。
本当にアイデアですが作ってみました
https://github.com/modeverv/ajaxremotecontroll

定期的にサーバー側のjsを読み込んでevalする、というものです。
作りこむと本気で使えるとおもえた。

取り込み部分

function evil_eval(){
    $.ajax({
               url: "./command.js",
               cache: false,
               crossDomain : true,
               dataType : "script",
               success: function(res){
                   if(com.session.match(SESSION)){
                       com.command.call();
                   }else{
                       console.log("for other session command.");
                   }
               }
           });    
}

var timer;
$(function(){
      timer = setInterval(evil_eval,5000);
  });

送り出し部分

var com = { 
    session : "xcxc,xxx",
    command : function(){

        if(!document.getElementById("hoge")){
            var html = "<div id=\"hoge\" style=\"position:absolute;top:0;left:0;width:200px;\"></div>";
            $("body").append(html);

        }

        var message = "a";
        $("#hoge").html($("#hoge").html() +"<br/>" + message );
        //$("#hoge").remove();
    }
};

html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <script src="jquery.js"></script>
    <script>
      var SESSION = 'xxx';
    </script>  
    <script src="remote.js"></script>
  </head>
  <body>
  </body>
</html>