-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMediaStream.js
More file actions
47 lines (42 loc) · 1.2 KB
/
useMediaStream.js
File metadata and controls
47 lines (42 loc) · 1.2 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
const WebSocket = require("ws");
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const wss = new WebSocket.Server({ server });
// Handle Web Socket Connection
wss.on("connection", function connection(ws) {
console.log("New Connection Initiated");
ws.on("message", function incoming(message) {
const msg = JSON.parse(message);
switch (msg.event) {
case "connected":
console.log(`A new call has connected.`);
break;
case "start":
console.log(`Starting Media Stream ${msg.streamSid}`);
break;
case "media":
console.log(`Receiving Audio...`);
break;
case "stop":
console.log(`Call Has Ended`);
break;
}
});
});
//Handle HTTP Request
app.get("/", (req, res) => res.send("Hello World"));
app.post("/", (req, res) => {
res.set("Content-Type", "text/xml");
res.send(`
<Response>
<Start>
<Stream url="wss://${req.headers.host}/"/>
</Start>
<Say>I will stream the next 60 seconds of audio through your websocket</Say>
<Pause length="60" />
</Response>
`);
});
console.log("Listening at Port 8080");
server.listen(8080);