-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (45 loc) · 1.38 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
const express = require ('express');
const http = require('http');
const cors = require('cors');
const axios = require('axios');
const socketIo = require('socket.io');
const index = require('./index');
const port = process.env.PORT || 4000;
const URL ='https://randomuser.me/api/?page=3&results=5&seed=abc';
const app = express();
app.use(index);
const server = http.createServer(app);
const io = socketIo(server);
console.log('ws is running');
io.on('connection', newConnection);
function newConnection(socket) {
console.log('new connection:' + socket.id);
socket.on('disconnect', () => {
console.log('Client disconnected:' + socket.id);
})
//interval = setInterval(() => getApiAndEmit(socket), 1000);
socket.on('fetch', (msg) => getApiAndEmit(msg, socket));
socket.on('fetch', input)
function input(data) {
console.log(data)
}
}
const getApiAndEmit = (msg, socket) => {
axios.get(URL)
.then(result => {
let idx = 0;
const interval = setInterval(() => {
if (idx >= result.data.results.length) {
clearInterval(interval);
} else {
console.log('sending data: ', idx);
socket.emit("image", result.data.results[idx]);
idx += 1;
}
}, 1000);
})
.catch(err => {
console.log('Error fetching images');
})
};
server.listen(port, () => console.log(`Listening on port ${port}`));