-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (39 loc) · 1.37 KB
/
index.js
File metadata and controls
51 lines (39 loc) · 1.37 KB
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
//dependencies
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//set default port when launch the app
var port = process.env.PORT || 3000;
//variable to store how many users online
var count = 0;
app.get('/', function(req,res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
//when a user online, +1 value to count
count++;
//send value of "count" to index.html, known as "count" variable
//index.html will start to retrieve data at event "calc"
io.emit('calc', {count : count});
console.log("client : " + count);
console.log('a user connected');
//notify that a user disconnected from chat
socket.on('disconnect', function(){
//check user disconnected
console.log('user disconnect');
//when a user offline, -1 value to count
count--;
io.emit('calc', {count : count});
console.log('user disconnect');
});
//when event "chat message" has run
socket.on('chat message', function(msg){
//display message on command prompt
console.log('message : ' + msg);
//return data message to index.html
io.emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening port ' + port);
});