[英]How to import the constant that is assigned a function result in javascript
[英]How to pipe a data stream into a function that's assigned to a constant?
以下来自Google的示例有效,但它使用pipe
。 就我的情况而言,我正在侦听一个以20ms为增量发送数据包的websocket
,而根据我的发现,无法将这些数据通过pipe
传递给函数。
初始化时必须传递的第一个参数是config
对象。 之后,仅接受数据。 所以我将函数设置为变量,然后传递配置。 但是我不知道以后如何将数据流传递给它。 我如何通过数据导入recognizeStream
不使用pipe
? 或者有没有办法在websocket
使用pipe
我可以通过在一定时间间隔内从临时文件读取和写入来保证该设置的正常运行,但是这样做的明显缺点是:1)所有这些开销,以及2)最重要的是,它不是实时流。
我认为可以使用两种解决方案,但无法实施:
websocket
设置pipe
(这是理想的) fs
某些实现使用createReadStream
从文件中读回数据(这似乎是问题的雷区) tl; dr我需要在数据传入时将来自websocket
的数据流发送到分配给const
中。
Google文档中的示例设置
const Speech = require('@google-cloud/speech');
// Instantiates a client
const speech = Speech();
// The encoding of the audio file, e.g. 'LINEAR16'
const encoding = 'LINEAR16';
// The sample rate of the audio file, e.g. 16000
const sampleRate = 16000;
const request = {
config: {
encoding: encoding,
sampleRate: sampleRate
}
};
const recognizeStream = speech.createRecognizeStream(request)
.on('error', console.error)
.on('data', (data) => process.stdout.write(data.results));
// Start recording and send the microphone input to the Speech API
record.start({
sampleRate: sampleRate,
threshold: 0
}).pipe(recognizeStream);
Websocket设置
const WebSocketServer = require('websocket').server
wsServer.on('connect', (connection) => {
connection.on('message', (message) => {
if (message.type === 'utf8') {
console.log(message.utf8Data)
} else if (message.type === 'binary') {
// send message.binaryData to recognizeStream
}
})
})
您应该能够:
recognizeStream.write(message.binaryData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.