Node.js WebSocket 服务器:使用 Youdao API 进行语音识别
import { nanoid } from 'nanoid'; import { createServer } from 'http'; import { Server } from 'ws';
const httpServer = createServer(); const wss = new Server({ server: httpServer });
httpServer.listen(3000, () => { console.log('Server is running on port 3000'); });
wss.on('connection', ws => { const appId = '0c1b5e4ca7aaa024' const secretKey = 'dPRBMN0Yskb3KvB5JWRTAFclDAmO6fu3'
const salt = nanoid()
const curtime = new Date()
const sign = sha256(appId + salt + curtime + secretKey)
const signType = 'v4'
const langType = 'zh-CHS'
const format = 'mp3'
const channel = '1'
const version = 'v1'
const rate = '16000'
const params = `appKey=${appId}&salt=${salt}&curtime=${curtime}&sign=${sign}&signType=${signType}&langType=${langType}&format=${format}&channel=${channel}&version=${version}&rate=${rate}`
console.log(params);
const youdaoWs = new WebSocket(`wss://openapi.youdao.com/stream_asropenapi?${params}`) //填服务器地址
youdaoWs.addEventListener('open', () => {
console.log('连接上服务器了');
})
// 服务器返回的数据
youdaoWs.addEventListener("message", ({ data }) => {
console.log(data);
})
ws.on('message',data => {
ws.send(data + 'hello')
})
ws.on('close',() => {
console.log('关闭连接了')
})
})
原文地址: https://www.cveoy.top/t/topic/pJ7z 著作权归作者所有。请勿转载和采集!