-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
88 lines (74 loc) · 2.32 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
82
83
84
85
86
87
88
require('dotenv/config')
const express = require('express');
const app = express();
const socketio = require('socket.io');
const http = require('http');
const server = http.createServer(app);
const io = socketio(server);
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 4000;
const cors = require('cors');
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
mongoose.connect(process.env.DBURL, {useNewUrlParser: true, useCreateIndex: true})
.then(() => console.log('Database is Connected...'))
.catch((err) => console.log(err));
//API Routes
app.use('/users',require('./routes/users.js'));
// Set static folder
app.use(express.static('./client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve( __dirname ,'client', 'build', 'index.html'));
});
const chatServices = {
NEW_MESSAGE:'Message From client to server',
SEND_MESSAGE:'Message From server to client',
CONNECTION:'connection',
DISCONNECT:'disconnect',
TYPING:'typing in fontend',
STOP_TYPING:'typing stopped in frontend',
NEW_USER:'register new user',
UPDATE_USERS:'Updating Online User'
}
var {NEW_MESSAGE,SEND_MESSAGE,CONNECTION,DISCONNECT,TYPING,STOP_TYPING,NEW_USER,UPDATE_USERS} = chatServices;
connections = [];
users = [];
//Online Chat Service
io.on(CONNECTION, (socket) => {
//Pushing socket
connections.push(socket);
console.log('Connected... : %s connected.',connections.length);
socket.on(DISCONNECT,() => {
connections.splice(connections.indexOf(socket.id),1);
console.log('Disconnected... : %s remaining.', connections.length);
console.log(socket.emailid);
users = users.filter(user => user.emailid!=socket.emailid);
updateContacts();
});
socket.on(NEW_MESSAGE,data => {
io.sockets.emit(SEND_MESSAGE,data);
});
socket.on(NEW_USER,(data,callback) => {
if(users.find( ele => data[1]===ele.emailid)){
callback(false);
}
else{
socket.emailid = data[1];
users.push({
username: data[0],
emailid: data[1]
});
updateContacts();
callback(true);
}
});
updateContacts = () =>{
io.sockets.emit(UPDATE_USERS,users);
}
});
server.listen(PORT, function(){
console.log('Server is running on Port: ',PORT);
});