forked from thomasysliu/node-paint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
107 lines (84 loc) · 2.37 KB
/
server.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Instantiate the main application variables
- Create HTTP-Server
- Bind an socket stream to the HTTP-Server
- Include file system methods
*/
var clickX = new Array();
var clickY = new Array();
var clickColor = new Array();
var clickTool = new Array();
var clickSize = new Array();
var clickDrag = new Array();
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs')
//var app = require('http').createServer(handler)
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
// Bind HTTP-Server on Port 80
//app.listen(8888);
server.listen(8888);
// Delivering the main site
function handler (req, res) {
fs.readFile(__dirname + '/index.html', function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// Initialize the socket stream
io.sockets.on('connection', function (socket) {
//setInterval(lineGenerator, 500, socket);
var i = 0;
for(; i < clickX.length; i++)
{
socket.emit('addClick', { x: clickX[i], y:clickY[i],Color:clickColor[i],Tool:clickTool[i],Size:clickSize[i],drag:clickDrag[i] });
}
socket.emit('mouseup');
console.log("AAAA");
socket.on('addClick', function (data) {
//console.log(data);
socket.broadcast.emit('addClick',data);
addClick_remote(data.x, data.y, data.drag ,data.Tool,data.Color,data.Size);
});
socket.on('mouseup', function (data) {
//console.log(data);
socket.broadcast.emit('mouseup');
});
});
// addClick the socket stream
/**
* Adds a point to the drawing array.
* @param x
* @param y
* @param dragging
*/
function addClick_remote(Rx, Ry, Rdragging,RcurTool,RcurColor,RcurSize)
{
//console.log(Rx,Ry,RcurColor,RcurTool);
clickX.push(Rx);
clickY.push(Ry);
clickTool.push(RcurTool);
clickColor.push(RcurColor);
clickSize.push(RcurSize);
clickDrag.push(Rdragging);
}
// Generating and sending a line
function lineGenerator(socket)
{
x_1 = Math.round(1 + 299*(Math.random()));
x_2 = Math.round(1 + 299*(Math.random()));
y_1 = Math.round(1 + 299*(Math.random()));
y_2 = Math.round(1 + 299*(Math.random()));
socket.emit('news', { x1: x_1, y1: y_1, x2: x_2, y2: y_2 });
}