forked from kbop543-zz/SettleWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (61 loc) · 1.95 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
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var Message = require('./models/message');
var User = require('./models/user');
var users = [];
var connections = [];
app.set('view engine', 'ejs');
app.use(express.static(__dirname));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
var chatController = require('./controllers/chatController');
var index = require('./controllers/indexController');
app.use('/',chatController);
app.use('/', index);
app.get('/mentee', function(req, res) {
res.render('mentee');
});
app.get('/mentor', function(req, res) {
res.render('mentor');
});
app.get('/confirmation', function(req, res) {
res.render('confirmation');
});
app.get('/fullChat', function(req, res) {
res.render('fullChat');
});
io.sockets.on('connection', function(socket){
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
//disconnect
socket.on('disconnect', function(data){
users.splice(users.indexOf(socket.username), 1);
updateUsernames();
connections.splice(connections.indexOf(socket), connections.length);
console.log('Disconnected: %s sockets connected', connections.length);
});
//send message
socket.on('send message', function(data){
console.log('data is ' + data);
io.sockets.emit('new message', {msg: data, user: socket.username});
});
socket.on('new user', function(data, callback){
callback(true);
socket.username = data;
console.log("user is " + socket.username);
users.push(socket.username);
updateUsernames();
});
function updateUsernames(){
io.sockets.emit('get users', users);
};
});
// Start the server
server.listen(3000);
console.log('Listening on port 3000');