initial commit
initial commit

file:b/.gitignore (new)
--- /dev/null
+++ b/.gitignore
@@ -1,1 +1,3 @@
+node_modules
+bower_components
 

file:b/bower.json (new)
--- /dev/null
+++ b/bower.json
@@ -1,1 +1,20 @@
+{
+  "name": "tracking",
+  "version": "0.0.1",
+  "authors": [
+    "Razvan Stanga <git@razvi.ro>"
+  ],
+  "moduleType": [
+    "node"
+  ],
+  "license": "MIT",
+  "ignore": [
+    "**/.*",
+    "node_modules",
+    "bower_components"
+  ],
+  "dependencies": {
+    "socket.io-client": "~1.4.4"
+  }
+}
 

file:b/index.js (new)
--- /dev/null
+++ b/index.js
@@ -1,1 +1,71 @@
+var express = require('express');
+var app = express();
+var http = require('http').Server(app);
+var io = require('socket.io')(http);
 
+app.get('/', function(req, res){
+    res.sendFile(__dirname + '/public/index.html');
+});
+
+app.get('/admin', function(req, res){
+    res.sendFile(__dirname + '/public/admin.html');
+});
+
+app.use(express.static(__dirname + '/public'));
+app.use(express.static(__dirname + '/bower_components'));
+
+var admin = [];
+
+io.on('connection', function(socket){
+    var mouse = [];
+    var color = "#"+((1<<24)*Math.random()|0).toString(16);
+    var id = socket.id;
+
+    socket.on('t:connect', function (msg) {
+        if (msg.admin == 1234) {
+            console.log ("admin connected " + id);
+            admin.push(id);
+        }
+        socket.emit('t:connected', {});
+    });
+
+    socket.on('disconnect', function () {
+        console.log ("admin disconnected " + id);
+        index = admin.indexOf(id);
+        if (index != -1) {
+            admin.splice(index, 1);
+        }
+    });
+
+    socket.on('t:mousemove', function(msg){
+        mouse.push(msg);
+        if (mouse.length > 2) {
+            mouse.shift();
+        }
+        if (mouse.length == 2) {
+            for (key in admin) {
+                io.to(admin[key]).emit('t:mousemove', mouse);
+            }
+        }
+    });
+
+    socket.on('t:click', function(click){
+        click.color = color;
+        for (key in admin) {
+            //console.log ("\tsend click to admin " + admin[key]);
+            io.to(admin[key]).emit('t:click', click);
+        }
+    });
+
+    socket.on('t:changemm', function(msg){
+        index = admin.indexOf(id);
+        if (index != -1) {
+            io.emit('t:changemm', {msg});
+        }
+    });
+});
+
+http.listen(3000, function(){
+    console.log('listening on *:3000');
+});
+

file:b/package.json (new)
--- /dev/null
+++ b/package.json
@@ -1,1 +1,10 @@
+{
+  "name": "tracking",
+  "version": "0.0.1",
+  "description": "tracking users mouse movments and clicks",
+  "dependencies": {
+    "express": "4.10.2",
+    "socket.io": "1.2.0"
+  }
+}
 

file:b/public/admin.html (new)
--- /dev/null
+++ b/public/admin.html
@@ -1,1 +1,16 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset='utf-8'>
+<meta content='IE=edge' http-equiv='X-UA-Compatible'>
+<meta content='width=device-width,initial-scale=1' name='viewport'>
+<title>Tracking</title>
+</head>
+<body>
+<div id="div"></div>
+<script type="text/javascript">
+    (function(i,s,o,g){a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','/js/a.js');
+</script>
 
+</body>
+</html>

file:b/public/index.html (new)
--- /dev/null
+++ b/public/index.html
@@ -1,1 +1,17 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset='utf-8'>
+<meta content='IE=edge' http-equiv='X-UA-Compatible'>
+<meta content='width=device-width,initial-scale=1' name='viewport'>
+<title>Tracking</title>
+</head>
+<body>
+<div id="div"></div>
+<script type="text/javascript">
+    (function(i,s,o,g){a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','/js/a.js');
+    (function(i,s,o,g){a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','/js/c.js');
+</script>
 
+</body>
+</html>

file:b/public/js/a.js (new)
--- /dev/null
+++ b/public/js/a.js
@@ -1,1 +1,73 @@
+function loadJs(i,s,o,g,c){
+    a=s.createElement(o);m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);
+    a.onload=function(){
+        c();
+    }
+}
+loadJs(window,document,'script','/socket.io-client/socket.io.js', function(){NRTTAdmin()});
 
+function NRTTAdmin(){
+    var animate = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
+            window.setTimeout(callback, 1000 / 60)
+    };
+    var canvas = document.createElement("canvas");
+    var width = 1920;
+    var height = 1080;
+    canvas.width = width;
+    canvas.height = height;
+    var ctx = canvas.getContext('2d');
+
+    var div = document.getElementById("canvas");
+    document.body.insertBefore(canvas, div);
+
+    var clicks = [];
+    var moves = [];
+
+    var step = function () {
+        render();
+        animate(step);
+    };
+
+    function render(){
+        ctx.clearRect(0, 0, width, height);
+        for(key in clicks) {
+            click = clicks[key];
+            if (click.time < Date.now()) {
+                clicks.splice(key, 1);
+            }
+            ctx.globalAlpha = 0.5;
+            ctx.beginPath();
+            ctx.fillStyle = click.color;
+            ctx.arc(click.x, click.y, 4, 0, 2 * Math.PI, true);
+            ctx.fill();
+            ctx.lineWidth = 0.5;
+            ctx.strokeStyle = '#003300';
+            ctx.stroke();
+        }
+        for(key in moves) {
+            move = moves[key];
+            if (move.time < Date.now()) {
+                moves.splice(key, 1);
+            }
+            ctx.beginPath();
+            ctx.moveTo(move[0].x, move[0].y);
+            ctx.lineTo(move[1].x, move[1].y);
+            ctx.stroke();
+        }
+    }
+
+    animate(step);
+
+    var aSocket = io();
+    aSocket.emit('t:connect', {w:window.width, h:window.height, admin:1234});
+    aSocket.on('t:click', function(click){
+        click.time = Date.now() + 15000;
+        clicks.push(click);
+    });
+    aSocket.on('t:mousemove', function(move){
+        move.time = Date.now() + 15000;
+        moves.push(move);
+    });
+    aSocket.on('disconnect', function(){
+    });
+}

file:b/public/js/c.js (new)
--- /dev/null
+++ b/public/js/c.js
@@ -1,1 +1,46 @@
+function loadJs(i,s,o,g,c){
+    a=s.createElement(o);m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);
+    a.onload=function(){
+        c();
+    }
+}
+loadJs(window,document,'script','/socket.io-client/socket.io.js', function(){NRTT()});
 
+function NRTT() {
+    var connected = false;
+
+    var mouseAction = {enabled: false, interval:100};
+    var clickAction = {enabled : true};
+
+    var cursorX, cursorY;
+    document.onmousemove = function(e){
+        cursorX = e.pageX;
+        cursorY = e.pageY;
+    }
+    document.onclick = function(e){
+        if (connected && clickAction.enabled) {
+            cSocket.emit('t:click', {x:e.pageX, y:e.pageY});
+        }
+    }
+    var cSocket = io();
+    cSocket.emit('t:connect', {w:window.width, h:window.height});
+    cSocket.on('t:connected', function(msg){
+        connected = true;
+    });
+    cSocket.on('disconnect', function(){
+        connected = false;
+    });
+    cSocket.on('t:changemm', function(msg){
+        mouseAction = msg;
+    });
+
+    cSocket.on('t:changec', function(msg){
+        clickAction = msg;
+    });
+
+    setInterval(function(){
+        if (connected && mouseAction.enabled) {
+            cSocket.emit('t:mousemove', {x:cursorX,y:cursorY});
+        }
+    }, mouseAction.interval);
+}

comments