forked from Marsview/Realtime-streams-playground-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·201 lines (173 loc) · 5.26 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
const express = require('express'); // const bodyParser = require('body-parser'); // const path = require('path');
const app = express();
const port = process.env.PORT || 1337;
const server = require('http').createServer(app);
const path = require('path');
const axios = require('axios');
var bodyParser = require('body-parser')
const ioClient = require('socket.io-client')
const session = require("express-session");
const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
const io = require('socket.io')(server);
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}))
// parse application/json
app.use(bodyParser.json())
io.use(wrap(session({
secret: "cats"
})));
app.use('/assets', express.static(__dirname + '/public'));
app.use('/session/assets', express.static(__dirname + '/public'));
// Inital state parameters
let mv_io_client = null;
let client = null;
let txnIdTemp = null;
let channelIdTemp = null;
let authParams = {
token: null,
txnId: null,
channelId: null,
modelConfigs: null,
}
// =========================== ROUTERS ================================ //
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/views/index.html'));
});
app.use('/', function (req, res, next) {
next(); // console.log(`Requests Url: ${req.url}`);
});
app.post('/get_access_token', function (req, res) {
if (req.body) {
// Post request configurations
let config = {
headers: {
"Content-Type": "application/json"
}
}
axios.post("https://api.marsview.ai/cb/v1/auth/create_access_token", {
apiKey: req.body.apiKey,
apiSecret: req.body.apiSecret,
userId: req.body.userId
}, config).then(response => {
if (response.data.status) {
res.json({
status: true,
token: response.data.data.accessToken
})
authParams.token = response.data.data.accessToken;
} else {
res.json(response.data)
}
})
}
})
app.post('/get_credentials', function (req, res) {
if (req.body) {
// Post request configurations
let config = {
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + authParams.token
}
}
axios.post("https://streams.marsview.ai/rb/v1/streams/setup_realtime_stream", {
channels: req.body.channelNumber
}, config).then(response => {
if (response.data.status) {
txnIdTemp = response.data.data.txnId;
channelIdTemp = response.data.data.channels[0].channelId;
res.json(response.data)
} else {
res.json(response.data)
}
})
}
})
app.put("/set_credentials", function (req, res) {
let model_configs = {
'intent_analysis': {
'intents':
["intent-bxllq2f7hpkrvtyzi3-1627981197627",
"intent-bxllq2f7hpkrvtzlkf-1627981226223"]
}
}
authParams.txnId = txnIdTemp;
authParams.channelId = channelIdTemp;
authParams.modelConfigs = model_configs;
// Initialize mv_io_client object
mv_io_client = ioClient.connect('https://streams.marsview.ai/', {
auth: authParams
}); // Marsview Realtime Server
initializeListeners();
res.json({
status: true
});
})
// Socket io connections
io.on('connection', function (clientLocal) {
client = clientLocal;
console.log("Client connected");
let absolute_start_time = new Date().getSeconds();
let recognizeStream = null;
let chunkMap = {}
client.on('join', function () {
client.emit('messages', 'Socket Connected to Server');
});
client.on('messages', function (data) {
console.log("Geetiing messages")
client.emit('broad', data);
});
});
function initializeListeners() {
client.on('startStream', function (data) {
mv_io_client.emit('startStream', data)
});
client.on('endStream', function () {
mv_io_client.emit('endStream')
});
client.on('binaryData', function (data) {
mv_io_client.emit('binaryData', data)
});
mv_io_client.on('messages', function (data) {
console.log("Getting messages")
client.emit('messages', data);
});
mv_io_client.on('valid-token', function (data) {
client.emit('valid-token', data);
});
mv_io_client.on('invalid-token', function (data) {
console.log("Invalid token")
client.emit('invalid-token', data);
});
mv_io_client.on('output', function (sentiment) {
client.emit('output', sentiment)
});
}
// The encoding of the audio file, e.g. 'LINEAR16'
// The sample rate of the audio file in hertz, e.g. 16000
// The BCP-47 language code to use, e.g. 'en-US'
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US'; //en-US
const request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
profanityFilter: false,
enableWordTimeOffsets: true,
// speechContexts: [{
// phrases: ["hoful","shwazil"]
// }] // add your own speech context for better recognition
},
interimResults: true, // If you want interim results, set this to true
};
// =========================== START SERVER ================================ //
server.listen(port, '127.0.0.1', function () {
//http listen, to make socket work
// app.address = "127.0.0.1";
console.log('Server started on port:' + port);
});