-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.49 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict'
const voiceEvent = async (req, res, next) => {
const { logger } = req.nexmo;
try {
logger.info("event", { event: req.body});
res.json({});
} catch (err) {
logger.error("Error on voiceEvent function")
}
}
const voiceAnswer = async (req, res, next) => {
const { logger } = req.nexmo;
logger.info("req", { req_body: req.body});
try {
return res.json([
{
"action": "talk",
"text": "Please wait while we connect you to the echo server"
},
{
"action": "connect",
"from": "NexmoTest",
"endpoint": [{
"type": "websocket",
"uri": `wss://${req.hostname}/socket`,
"content-type": "audio/l16;rate=16000",
}]
}
]);
} catch (err) {
logger.error("Error on voiceAnswer function");
}
}
const route = (app, express) => {
const expressWs = require('express-ws')(app);
const WebSocket = require('ws');
expressWs.getWss().on('connection', function (ws) {
console.log('Websocket connection is open');
});
// websocket middleware
app.ws('/socket', (ws, req) => {
ws.on('message', (msg) => {
setTimeout(() => {
if (ws.readyState === WebSocket.OPEN) ws.send(msg);
}, 500);
});
});
};
module.exports = {
voiceEvent,
voiceAnswer,
route
}