This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
120 lines (100 loc) · 3.04 KB
/
index.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./helper/db');
const mongo = require('mongodb');
const ObjectId = mongo.ObjectID;
const axios = require('axios');
const http = require('http');
const port = process.env.PORT || 3000;
const app = express();
const server = http.createServer(app);
const socketIo = require('socket.io');
const io = socketIo.listen(server);
const cookieParser = require('cookie-parser');
const session = require('express-session')({
secret: process.env.SECRET,
resave: true,
saveUninitialized: false,
cookie: {
maxAge: 3600000,
},
});
app.use(express.static(__dirname + '/views'));
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.set('socketio', io);
app.use(session);
const sharedsession = require('express-socket.io-session');
io.use(sharedsession(session));
app.use(require('./routes/routes'));
// realtime chat
io.on('connection', (socket) => {
console.log(`A new client connected: ${socket.id}`);
const socketRoom = socket.handshake.session.chatroom;
socket.join(socketRoom);
socket.on('message', (data) => {
const sender = socket.handshake.session.user._id;
const str = data.message.trim()
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
const databaseData = {
sender: sender,
content: str,
time: new Date(),
media: data.media,
};
// send to database
db.get()
.collection('chats')
.updateOne(
{
_id: ObjectId(socketRoom),
},
{$push: {messages: databaseData}},
);
socket.to(socketRoom).emit('message', str);
});
socket.on('gif', async (data) => {
const sender = socket.handshake.session.user._id;
const str = data.message.trim();
const databaseData = {
sender: sender,
content: str,
time: new Date(),
media: data.media,
};
const url = `https://api.giphy.com/v1/gifs/search?api_key=${process.env.GIPHY_APIKEY}&limit=5&q=${str}`;
const response = await axios.get(url);
const index = Math.floor(Math.random() * response.data.data.length);
if (response.data.data[index]) {
databaseData.media = 'gif';
databaseData.content = response.data.data[index].images.downsized.url;
} else {
databaseData.content =
'https://media.giphy.com/media/H7wajFPnZGdRWaQeu0/giphy.gif';
}
// send to database
db.get()
.collection('chats')
.updateOne(
{
_id: ObjectId(socketRoom),
},
{$push: {messages: databaseData}},
);
socket.emit('hello', databaseData.content);
socket.to(socketRoom).emit('gif', databaseData.content);
});
socket.on('disconnect', () => {
console.log('Client has disconnected');
});
});
db.connect(() => {
server.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
});