-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
164 lines (132 loc) Β· 3.84 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const express=require('express');
const session=require('express-session');
//load environment variables
require('dotenv').config()
//Body Parser to set linit to req size
const bodyparser=require('body-parser');
//import from API folder
const App=require('./API/post')
let tempChatData={}
//Socket files
const socketio=require('socket.io')
const http=require('http')
const app=express()
const server=http.createServer(app)
const io=socketio(server)
//Peoples
let map={};
let mapAlive={};
let revmap={};
let session_username_map=App.session_username_map;
let user_name;
app.use(bodyparser.json({limit:'2MB'}))
app.use(bodyparser.urlencoded({
extended:true,
limit:'2MB'
}))
app.use(session({
secret:'this is MY secret!!!!',
resave:false,
saveUninitialized:true,
cookie:{
maxAge:24*60*60*1000
}
}))
//Socket Connections
{
io.on('connection',(socket)=>{
socket.on('msgfor',(data)=>{
console.log(data);
io.to(revmap[data.name]).emit('incoming',{
from:map[socket.id],
message:data.message
})
//save chat to server
if(!tempChatData[data.name])
tempChatData[data.name]={};
if(!tempChatData[data.name][map[socket.id]])
tempChatData[data.name][map[socket.id]]={};
tempChatData[data.name][map[socket.id]][Date.now()]=data.message;
socket.emit('unread',tempChatData);
})
socket.emit('unread',tempChatData)
socket.on('unread',()=>{
socket.emit('unread',tempChatData)
})
socket.on('received',(data)=>{
//clear chat stored on server
tempChatData[data.name][data.from]={}
})
socket.on('isOnline',(data)=>{
socket.emit('online',{
name:data.name,
answer:mapAlive[data.name]
})
})
socket.on('isAlive',(data)=>{
let username=session_username_map[data.session];
let socketid=socket.id;
mapAlive[username]=true;
revmap[username]=socketid;
map[socketid]=username;
console.log(mapAlive);
broadcast_online(username);
console.log(username+" online");
})
socket.on('disconnect',()=>{
let socketid=socket.id;
let username=map[socketid];
if(username)
{
console.log(username+"offline");
delete map[socketid];
delete revmap[username];
delete mapAlive[username];
broadcast_offline(username);
}
})
function broadcast_online(username){
io.emit('online',{
name:username,
answer:(mapAlive[username])?true:false
})
}
function broadcast_offline(username){
io.emit('offline',{
name:username
})
}
})
}
//Configuration
{
app.set('view engine','hbs')
app.use((req,res,next)=>{
if(!req.session.user){
//console.log("New user")
req.session.user=true;
req.session.logged=false;
return res.redirect('/login');
}
else
next();
})
app.get('/parasnagpal',(req,res)=>{
res.render('/login')
})
app.use(express.static(__dirname+'/views'));
app.use(express.static(__dirname+'/chat'));
app.use('/',App.app);
app.use('/',require('./views/routes/route'));
}
function send_message(username,message){
io.on('connection',(socket)=>{
socket.emit('msgfor',{
name:username,
message:message
})
})
}
server.listen(process.env.PORT||4000,()=>{
console.log('Server started at http://localhost:4000')
})