This repository was archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (73 loc) · 2.39 KB
/
Copy pathapp.js
File metadata and controls
87 lines (73 loc) · 2.39 KB
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
import express from 'express';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { createServer } from 'http';
import { Server } from 'socket.io';
import { Twitch } from './twitch.js';
const twitch = new Twitch()
const port = process.env.PORT || 5353
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express()
const server = createServer(app)
const io = new Server(server, {
path: '/clippy/socket.io'
})
// EXPRESS ROUTING
app.use('/clippy', express.static(__dirname + '/public')); // /clippy will point to all the needed backend files
app.use((_, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/clippy', (_, res) => {
console.log('page not found')
res.sendStatus(404);
});
app.get('/clippy/getclips', async (req, res) => {
const username = req.query.channel;
const limit = req.query.limit;
if(!username || !limit) {
res.sendStatus(400);
return;
}
const id = await twitch.getUserID(username);
const clips = await twitch.getClips(id, limit);
res.json({
clips: clips.data
});
res.end();
})
app.get('/clippy/:host', (req, res) => {
if(Object.keys(req.query).length === 0 && req.params.host) {
res.sendFile('index.html', { root: __dirname });
} else {
const host = req.params.host
// emit to a specific room only so we don't need to worry about client-side checking
// io.to(host).emit('shoutout', { streamer: req.query.streamer, duration: req.query.duration })
io.to(host).emit('shoutout', { streamer: req.query.streamer, duration: req.query.duration })
console.log(`Sent shoutout event to room #${host}`)
res.end();
}
});
// SOCKET.IO LISTENERS
io.on('connection', (socket) => {
const room = socket.handshake['query']['host']
if (room) {
socket.join(room)
console.log(`User joined #${room}`)
socket.on('disconnect', () => {
socket.leave(room)
console.log('user disconnected')
})
} else {
// disconnect?
socket.disconnect()
}
socket.on('disconnect', () => {
console.log('user disconnected');
})
})
server.listen(port, () => {
console.log(`Listening on port: ${port}`)
})