-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (84 loc) · 2.22 KB
/
app.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
var app = require('express')();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var port = 3000;
server.listen(port);
console.log('se conecte na: http://localhost:' + port);
//Public web
app.get('/', function (req, res) {
res.sendfile(path.join(__dirname, '/index.html'));
});
app.get('/layout2', function (req, res) {
res.sendfile(path.join(__dirname, '/index2.html'));
});
app.get('/css/*', function(req, res){
res.sendfile(path.join(__dirname, '/css/', req.params[0]));
});
app.get('/js/*', function(req, res){
res.sendfile(path.join(__dirname, '/js/', req.params[0]));
});
app.get('/images /*', function(req, res){
res.sendfile(path.join(__dirname, '/images/', req.params[0]));
});
app.get('/sounds/*', function(req, res){
res.sendfile(path.join(__dirname, '/sounds/', req.params[0]));
});
var setUserRoom = function(obj){
debugger;
rooms.forEach(function (e,i){
if(e.id == obj.room){
rooms[i].usersIn.push(obj);
}
})
}
var sendMsgToRoom = function (obj){
debugger;
rooms.forEach(function (e,i){
if(e.id == obj.user.room){
e.usersIn.forEach(function (e2,i2){
if(e2.id != obj.user.id){
io.sockets.socket(e2.id).emit('messageToClient', obj.msg);
}
});
}
})
}
//Sockets
var users = [];
var rooms = [];
var idRoom = 1;
rooms.push(
{
id:idRoom,
name:'Geral',
description:'Ajustos em geral',
color1:'#e5717d',
color2:'#c9636e',
color3:'#e8828d',
public: true,
password: '',
usersIn:[]
}
);
io.sockets.on('connection', function (socket) {
socket.on('message', function (obj) {
debugger;
sendMsgToRoom(obj)
});
socket.on('connect', function() {
debugger;
var userCoon = {id:this.id, room:1};
users.push(userCoon);
setUserRoom(userCoon);
var nConn = io.rooms[''].length;
socket.broadcast.emit('countusers', nConn);
socket.emit('countusers', nConn);
socket.emit('connectUser', userCoon);
});
socket.on('disconnect', function() {
var nConn = io.rooms[''].length -1;
socket.broadcast.emit('countusers', nConn);
socket.emit('countusers', nConn);
});
});