-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws-server.js
More file actions
71 lines (57 loc) · 2.22 KB
/
ws-server.js
File metadata and controls
71 lines (57 loc) · 2.22 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
import { createServer } from 'node:http';
import next from 'next';
import { Server } from 'socket.io';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 5001;
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server(httpServer);
// Directory to save uploaded files
const UPLOAD_DIR = path.join(__dirname, 'uploads');
if (!fs.existsSync(UPLOAD_DIR)) {
fs.mkdirSync(UPLOAD_DIR);
}
io.on('connection', (socket) => {
console.log('Client connected');
socket.on("tracks_state", (receivedMsg) => {
// console.log("recieved: ", receivedMsg)
console.log("received a msg ")
io.sockets.emit("tracks_state", receivedMsg);
});
// Handle audio upload event
socket.on('audio_chan', (data) => {
const { filename, audio_data } = data;
// const filePath = path.join(UPLOAD_DIR, filename);
// // Decode the base64 string and write to file
// const audioBuffer = Buffer.from(audio_data.split(',')[1], 'base64');
// fs.writeFile(filePath, audioBuffer, (err) => {
// if (err) {
// console.error('Error saving audio file:', err);
// return;
// }
// console.log(`Audio file ${filename} saved successfully`);
// // Emit a confirmation event back to the client
// io.sockets.emit('audio_chan', data);
// });
console.log("received an audio file", data.filename)
io.sockets.emit('audio_chan', data);
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
httpServer.once('error', (err) => {
console.error(err);
process.exit(1);
}).listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});