-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (33 loc) · 1.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
const fs = require("fs");
const path = require("path");
const http = require("http");
const express = require("express");
const { Server } = require('socket.io')
const app = express();
const PORT = process.env.PORT || 3000;
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
app.use(express.static(path.join(__dirname, "./public")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
io.on("connection", (client) => {
console.log("New websocket connection!...");
client.on("transcribe", (data) => {
const dataURL = data.split(",").pop();
// convert to buffer
let fileBuffer = Buffer.from(dataURL, "base64");
fs.writeFileSync(`${client.id}.wav`, fileBuffer);
});
client.on("disconnect", (reason) => {
console.log(`\nDisconnected....\nReason=${reason}\n`);
});
});
server.listen(PORT, () => {
console.log(`Listening on PORT: ${PORT}`);
});