-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (57 loc) · 1.86 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
//Include express
const express = require('express');
//Include path
const path = require('path');
//Create app variable as express
const app = express();
//Create the server
const server = require('http').createServer(app);
//Include the socket.io
const io = require('socket.io').listen(server);
//Set the port
const port = 3000;
//Create an empty array of users
let users = [];
//VIew engine setup
app.set('views', path.join(__dirname,'views'));
app.set('view engine','pug');
//Setting the static path
app.use(express.static(path.join(__dirname, 'public')));
//Socket.io connect
io.sockets.on('connection', (socket) => {
//Set the username ie the data which we r getting
socket.on('set user',(data, callback)=>{
//check if the username exists in the array
if(users.indexOf(data) != -1){
callback(false);
}else{
callback(true);
socket.username = data;
users.push(socket.username);
updateUsers();
}
});
socket.on('send message', (data) => {
io.sockets.emit('show message', {msg: data, user:socket.username});
});
socket.on('disconnect', (data)=>{
if(!socket.username)
return;
//splice is used to remove the element from the last of the array
users.splice(users.indexOf(socket.username) , 1);
updateUsers();
});
//to create back and forth communication we did this
function updateUsers(){ //users ie 2nd argument is the array
io.sockets.emit('users', users); //this will emit the name back to the client
}
});
//Index route
app.get('/' ,(req,res,next) => {
//res.send('test');
res.render('index');
});
//It will indefintely listen to this port
server.listen(port, () =>{
console.log("Server started at port "+ port);
});