diff options
author | tomsmeding <hallo@tomsmeding.nl> | 2015-10-23 17:44:46 +0200 |
---|---|---|
committer | tomsmeding <hallo@tomsmeding.nl> | 2015-10-23 17:44:46 +0200 |
commit | 39901aa8410680de099e40cfb5577ca3cc4a13a3 (patch) | |
tree | 40e696f5a928f9a5f68c9abe8aedf516da011b1b /interactor | |
parent | aa3ec5e60581f65ff0106b7fbe58549ba1b568d3 (diff) |
42
Diffstat (limited to 'interactor')
-rw-r--r-- | interactor/index.html | 11 | ||||
-rwxr-xr-x | interactor/interactor.js | 36 |
2 files changed, 41 insertions, 6 deletions
diff --git a/interactor/index.html b/interactor/index.html index ff2edf4..19dc703 100644 --- a/interactor/index.html +++ b/interactor/index.html @@ -11,6 +11,9 @@ var CVSH=500; var COLOURS=["#00F","#F00","#0CC"]; var socket=io(); +setInterval(function(){ + socket.emit("ping"); +},1000); var CELLSZ=~~(CVSH/(H+1)); var CVSW=CELLSZ*(W+1); var CELL0X=~~(CVSW/2-W/2*CELLSZ)+.5,CELL0Y=~~(CVSH/2-H/2*CELLSZ)+.5; @@ -65,7 +68,7 @@ function drawboard(bd,clr){ for(x=0;x<W;x++){ ctx.fillStyle=COLOURS[bd[y][x].c]; angle=1/bd[y][x].n*2*Math.PI; - radius=bd[y][x].n==1?0:.12; + radius=bd[y][x].n==1?0:.15; for(i=0;i<bd[y][x].n;i++){ ctx.beginPath(); ctx.arc( @@ -169,8 +172,8 @@ function applymove(idx,c){ } } time++; - },30); - },500*i+1); + },20); + },350*i+1); }); } @@ -216,7 +219,7 @@ socket.on("getusermove",function(){ else getusermove(); }); socket.on("win",function(player){ - setstatustext("<b>Player "+(player+1)+" won</b>"); + setstatustext("<b><i>Player "+(player+1)+" won!</i></b>"); }); </script> </head> diff --git a/interactor/interactor.js b/interactor/interactor.js index 8a8a32f..32f8517 100755 --- a/interactor/interactor.js +++ b/interactor/interactor.js @@ -36,25 +36,56 @@ app.get("/",function(req,res){ }); }); +var childpids=[]; + +process.on("exit",function(){ + console.log("Exiting:"); + console.log.apply(console,arguments); + childpids.forEach(function(pid){process.kill(pid);}); + process.exit(); +}); +process.on("SIGINT",function(){ + console.log("SIGINT:"); + console.log.apply(console,arguments); + childpids.forEach(function(pid){process.kill(pid);}); + process.exit(); +}); + function stopproc(proc){ + var pid=proc.pid; + console.log("Stopping proc with pid "+pid+", childpids="+JSON.stringify(childpids)); proc.stdin.write("Quit\n"); var timeout=setTimeout(function(){ + console.log("Proc with pid "+pid+" didn't stop in time, killing"); proc.kill(); + var idx=childpids.indexOf(pid); if(idx!=-1)childpids.splice(idx,1); },500); proc.on("exit",function(){ clearTimeout(timeout); + var idx=childpids.indexOf(pid); if(idx!=-1)childpids.splice(idx,1); }); } io.on("connection",function(conn){ var id=uniqid(); console.log("New IO connection id "+id); + var pingtimeout=null; var bd=emptyboard(); var aiplayer=0; conn.on("disconnect",function(){ console.log("Disconnect id "+id); + if(proc){stopproc(proc);proc=null;} + if(pingtimeout)clearTimeout(pingtimeout); + }); + conn.on("ping",function(){ //only enable ping timeouting if a first ping received + if(pingtimeout)clearTimeout(pingtimeout); + pingtimeout=setTimeout(function(){ + console.log("Ping timeout on id "+id+"!"); + if(proc){stopproc(proc);proc=null;} + conn.disconnect(true); + },4000); }); conn.on("usermove",function(idx){ idx=+idx; @@ -71,7 +102,7 @@ io.on("connection",function(conn){ var win=checkwin(bd); if(win!=-1){ conn.emit("win",1-aiplayer); - stopproc(proc); + if(proc){stopproc(proc);proc=null;} return; } } @@ -83,6 +114,7 @@ io.on("connection",function(conn){ var proc=spawn("sh",["-c",aicmd],{ stdio:["pipe","pipe",process.stderr] }); + childpids.push(proc.pid); var buffer=""; proc.stdout.on("data",function(data){ var idx,line,mv,win; @@ -105,7 +137,7 @@ io.on("connection",function(conn){ win=checkwin(bd); if(win!=-1){ conn.emit("win",aiplayer); - stopproc(proc); + if(proc){stopproc(proc);proc=null;} return; } } |