-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (99 loc) · 3.18 KB
/
app.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
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const cookie = require('cookie');
const { Server } = require('socket.io');
const Storage = require('./backend');
const PORT = 80;
const app = express();
const server = http.createServer(app);
const ws = new Server(server);
const storage = new Storage();
app.use(bodyParser.urlencoded());
app.set('views','./views');
app.set('view engine','jade');
function getCookie(req,key) {
return cookie.parse(req.headers.cookie)[key];
}
app.use('/lib',express.static('./node_modules'));
app.use('/', express.static('./public'));
app.get('/', (req, res) => {
res.render("index");
});
app.post('/', (req, res) => {
let uname = req.body.uname;
if (!storage.isUsernameInUse(uname)) {
storage.registerUser(uname);
}
res.cookie('uname', uname);
res.redirect('/chat');
});
app.get('/chat/:roomname?', (req, res) => {
let roomname = req.params.roomname;
let uname = getCookie(req,'uname');
let rooms = storage.getAllMyRooms(uname);
let room = undefined;
if (roomname) {
room = storage.getRoom(roomname);
res.cookie('roomname',roomname);
}
else {
res.clearCookie('roomname');
}
res.render('chat', {uname,rooms,room});
});
app.get('/createroom', (req,res) => {
let currentuser = getCookie(req,'uname');
let users = storage.getAllUser().filter((e) => e != currentuser);
res.render('createroom',{uname: getCookie(req,'uname'), users});
});
app.post('/createroom', (req,res) => {
let currentuser = getCookie(req,'uname');
let roomname = req.body.room_name;
let participants = req.body.participants;
if (undefined == participants ||storage.isRoomExists(roomname)) {
res.redirect('/createroom');
}
else {
participants.push(currentuser);
storage.createRoom(roomname,currentuser,participants);
participants.forEach(p => {
if (p!=currentuser) {
let client = storage.getClient(p);
if (client) client.socket.emit('createroom',{roomname});
}
})
res.redirect('/chat');
}
});
app.get('/logout', (req,res) => {
res.clearCookie('uname');
res.clearCookie('roomname');
res.redirect('/');
})
/** WebSocket */
ws.on('connection', (socket) => {
let uname = getCookie(socket.handshake,'uname');
let client = { socket, uname };
console.log("connected: " + uname);
storage.addClient(uname, client);
socket.on('send',(chat) => {
let room = storage.addChat(chat.room,chat);
if (null != room) {
room.participants.forEach(p => {
if (p==chat.sender) return;
let client = storage.getClient(p);
if (client) {
client.socket.emit('receive',chat);
}
})
}
});
socket.on('disconnect', () => {
console.log('diconnected: ' + uname);
storage.removeClient(client);
});
});
server.listen(PORT, () => {
console.log("listening on: *:"+PORT);
})