-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (56 loc) · 2.02 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
const express = require("express");
const http = require("http");
const path = require("path");
const socketio = require("socket.io");
const axios = require("axios");
const app = express();
const server = http.createServer(app);
const io = socketio(server);
//Set static folder
app.use(express.static(path.join(__dirname, "public")));
const cachedGifs = {};
const gifHost = "https://api.giphy.com/v1/gifs";
const gifAPIKey = "biYm2PH6hsZ3FfM30QL9gGRInTxht4nv";
var gifLimit = 50;
var gifOffset = 0;
var gifRating = "PG-13";
var gifLang = "en";
//Run when client connects
io.on("connection", socket => {
socket.on("letter", letter => {
io.emit("leaf", letter);
});
//https://api.giphy.com/v1/gifs/search?api_key=biYm2PH6hsZ3FfM30QL9gGRInTxht4nv&q=child&limit=1&offset=0&rating=R&lang=en
socket.on("words", words => {
const startTime = Number(new Date().getTime());
for (word of words) {
if (cachedGifs[word] === undefined) {
axios
.get(
`${gifHost}/search?api_key=${gifAPIKey}&q=${encodeURI(
word
)}&limit=${gifLimit}&offset=${gifOffset}&rating=${gifRating}&lang=${gifLang}`
)
.then(({ data }) => {
if (data.data.length > 0) {
cachedGifs[word] = data.data.map(
item => item.images.preview_gif.url
);
const randomIndex = Math.floor(
Math.random() * cachedGifs[word].length
);
io.emit("gif", cachedGifs[word][randomIndex]);
socket.emit("time", Number(new Date().getTime()) - startTime);
console.log("Gif emitted: ", cachedGifs[word]);
}
});
} else {
const randomIndex = Math.floor(Math.random() * cachedGifs[word].length);
io.emit("gif", cachedGifs[word][randomIndex]);
socket.emit("time", Number(new Date().getTime()) - startTime);
}
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));