-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (85 loc) · 2.62 KB
/
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//npm install socket.io --msvs_version=2012 to install
var app = require('http').createServer();
var io = require('socket.io').listen(app, { log: false });
var fs = require('fs');
app.listen(8080);
counter = 1;
connections = [];
initiator = {};
started = 0;
recieved = 0;
/* --------------------------------------------------
When a user connects to the socket.io
-----------------------------------------------------*/
io.sockets.on('connection', function (socket) {
/* ----------------------------
On Register
----------------------------*/
socket.on('register', function (status, fn) {
register(status,fn,this);
});
register = function(status,fn,socket) {
//If we are registering
if (status) {
socket.number = "c" + counter++;
//Now store the socket in an array
connections.push({
num: socket.number,
socket: socket
});
console.log(socket.number + " Registered. " + connections.length + " connected");
}
//If we are disabling, we need to remove the socket
else if (socket.number.length) {
//Remove the one from the array
newConnections = [];
for(i=0; i < connections.length; i++){
if (connections[i].num != socket.number){
newConnections.push(connections[i]);
}
}
connections = newConnections;
console.log(socket.number + " Unregistered. " + connections.length + " connected");
socket.number = "";
}
//Do the required callback
if (typeof fn == "function"){
fn(socket.number);
}
//Tell the number of users how many are connected
io.sockets.emit('connections', { total: connections.length });
}
/* ----------------------------
On disconnect
----------------------------*/
socket.on("disconnect", function(){
register(false,null,this);
});
/* ----------------------------
On Request to compute
----------------------------*/
socket.on("request", function(data){
initiator = this;
//TODO, calculate required start/end points
//So first, lets figure out start/end points
started = new Date().getTime();
//Loop though all the connections
for(i=0;i < connections.length; i++){
console.log(i);
//Send to the socket required
connections[i].socket.emit('compute',{
size: data.size,
start: 0, //Always start from 0 now
end: data.size * data.size,
client: i
});
}
});
/* ----------------------------
On data response
----------------------------*/
socket.on("response", function(data){
initiator.emit("results", data);
recieved++;
});
});