mouseover on clicks will show client data
[realtime-mouse-tracking.git] / index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
 
app.get('/admin', function(req, res){
    res.sendFile(__dirname + '/dist/admin.html');
});
 
app.use(express.static(__dirname + '/dist'));
app.use(express.static(__dirname + '/bower_components'));
 
var admin = [];
var resolutions = [];
 
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);
        } else {
            console.log ("user connected " + id);
        }
        if (msg.color != null) {
            color = msg.color;
            socket.emit('t:connected', {});
        } else {
            socket.emit('t:connected', {color:color});
        }
        var resolution = msg.w + 'x' + msg.h;
        index = resolutions.indexOf(resolution);
        if (index == -1) {
            resolutions.push(resolution);
            for (key in admin) {
                io.to(admin[key]).emit('t:resolutions', resolutions);
            }
        }
    });
 
    socket.on('disconnect', function () {
        index = admin.indexOf(id);
        if (index != -1) {
            console.log ("admin disconnected " + id);
            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) {
            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(80, '192.168.0.3', function(){
    console.log('listening on 192.168.0.3:80');
});
 
comments