-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (45 loc) · 1.52 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
const app = require("express")();
// const bodyParser = require('body-parser')
const server = app.listen(8000, () => {
console.log("Server listening on port 8000");
});
const io = require('socket.io')(server);
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', require('./router.js'));
let numUsers = 0;
io.on('connection', function (socket) {
socket.on('add user', (username) => {
if (socket.hasOwnProperty('username')) return;
// console.log(username + ' connected ');
// store the username in the socket session for this client
socket.username = username;
numUsers+=1;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});
socket.on('send message', function (msg) {
io.emit('new message', {
'message':msg,
'username': socket.username,
});
});
// when the user disconnects.. perform this
socket.on('disconnect', () => {
// console.log(socket.username+' left')
if (socket.username && numUsers) {
numUsers -=1 ;
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});