-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
185 lines (133 loc) · 4.18 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*var express = require('express');
var app = express();
var server = app.listen(3000);
app.use(express.static('public'));
console.log("Server is running");
var socket = require('socket.io');
var io = socket(server);
io.sockets.on('connection', newConnection);
function newConnection(socket){
console.log('new connection' + socket.id);
socket.on('mouse', mouseMsg);
function mouseMsg(data){
socket.broadcast.emit('mouseStage', data); //emits to everyone but socket
//io.sockets.emit('mouseStage', data); //emits to everyone
console.log(data);
}
}
*/
'use strict';
const app = require("express")();
const express = require("express");
const http = require('http').Server(app);
const io = require("socket.io")(http);
const PORT = process.env.PORT || 3000;
// ========== Pages ========== //
// Allows acess to all files inside 'public' folder.
app.use(express.static(__dirname + "/public"));
// Configures each link to a different page.
// e.g. localhost:3000/ will load index.html
// e.g. localhost:3000/led will load led.html
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/stage.html');
});
// Hosts the page on port 3000
http.listen(PORT,function(){
console.log("Listening on " + PORT);
});
//======== sock-osc
/*'use strict';
const express = require('express');
const socketIO = require('socket.io');
const path = require('path');
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'public/index.html');
const server = express()
.use((req, res) => res.sendFile(INDEX) )
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('Client connected');
socket.on('disconnect', () => console.log('Client disconnected'));
});*/
//=====
//======== sock-osc
/*var PORT = process.env.PORT || 8080;
app.get('/',function(req,res){
res.sendFile(__dirname + '/osc.html');
});
http.listen(PORT,function(){
console.log('Server listen at port %s',PORT);
});*/
//====
// ========== SOCKET.IO ========== //
//var users = [], usersIndex = {};
var nsp = io.of('/stage');
nsp.on('connection', function(socket){
console.log('Stage is up at ' + socket.id);
});
//nsp.emit('hi', 'everyone!');
io.sockets.on('connection', newConnection);
function newConnection(socket){
console.log('new connection' + socket.id);
var sockID = socket.id;
socket.emit('sockID', sockID);
/////// Setting random color for socket
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
var color = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)];
socket.emit('color', color);
/////// end Setting random color for socket
//var index3 = undefined;
/////// Recieving data from socket
socket.on('mouse', mouseMsg);
function mouseMsg(data){
//console.log(data.x1);
nsp.emit('mouseStage', data);
//socket.broadcast.emit('mouseStage', data); //emits to everyone but socket
//io.sockets.emit('mouseStage', data); //emits to everyone
//console.log(data);
//users[socket.id] = [data];
/*var index2 = undefined;
addOrReplace(data);
function addOrReplace(object){
var index = usersIndex[object.i]; //takes id and assigns an index #
//console.log(index);
if(index === undefined){
index = users.length;
usersIndex[object.i] = index;
}
users[index] = object;
//console.log(index);
index2 = index;
return index2;
}
nsp.emit('mouseStage', data);
nsp.emit('tempPart', data);
//console.log(index2);
//console.log(users.length);
//console.log(users);
index3 = index2;
return index3;*/
}
/////// end Recieving data from socket
////// Disconnection
/*function deleteFromArray(users, index3) {
position = users.indexOf(index3);
users.splice(position, 1);
}*/
socket.on('disconnect', function () {
//console.log(index3);
//deleteFromArray(users, index3);
//console.log('disconnection' + socket.id);
nsp.emit('disconnect', sockID);
//console.log(users.length);
});
////// end Disconnection
}