-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (36 loc) · 1.09 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
const fs = require('fs');
const path = require('path');
// socket setup
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const port = process.env.PORT || 3000;
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const soundPath = path.join(__dirname, 'public', 'sounds');
const sounds = () => fs.readdirSync(soundPath).map(file => file.split('.').shift());
app.use(express.static(path.join(__dirname, 'build')));
app.get('/api/sounds', (req, res) => {
res.json(sounds());
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
io.on('connection', socket => {
console.log('user has connected');
socket.on('join', id => {
socket.join(id);
socket.to(id).emit('userJoined');
});
socket.on('soundRequest', data => {
const { sound, id } = data;
socket.to(id).emit('soundReceived', sound);
});
socket.on('disconnect', () => {
console.log('user has disconnected');
});
});
server.listen(port, () => {
console.log(`active on ${port}`);
});