Feat: 使用 Stomp 协议发送内容

This commit is contained in:
Paul 2024-06-04 14:37:55 +08:00
parent a7a1496ca2
commit 410183720b
4 changed files with 49 additions and 1 deletions

View File

@ -10,6 +10,7 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"@stomp/stompjs": "^7.0.0",
"mqtt": "^5.7.0", "mqtt": "^5.7.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",

View File

@ -5,6 +5,9 @@ settings:
excludeLinksFromLockfile: false excludeLinksFromLockfile: false
dependencies: dependencies:
'@stomp/stompjs':
specifier: ^7.0.0
version: 7.0.0
mqtt: mqtt:
specifier: ^5.7.0 specifier: ^5.7.0
version: 5.7.0 version: 5.7.0
@ -727,6 +730,10 @@ packages:
dev: true dev: true
optional: true optional: true
/@stomp/stompjs@7.0.0:
resolution: {integrity: sha512-fGdq4wPDnSV/KyOsjq4P+zLc8MFWC3lMmP5FBgLWKPJTYcuCbAIrnRGjB7q2jHZdYCOD5vxLuFoKIYLy5/u8Pw==}
dev: false
/@types/babel__core@7.20.5: /@types/babel__core@7.20.5:
resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==} resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==}
dependencies: dependencies:

View File

@ -5,6 +5,7 @@ import "./App.css";
import { client, start } from "./utils/mqtt"; import { client, start } from "./utils/mqtt";
import { startAudio, stopAudio, context } from "./utils/microphone"; import { startAudio, stopAudio, context } from "./utils/microphone";
import { arrayBufferToBase64, downSampleAudioFrame, floatTo16BitPCM, getMergedPCMData } from "./utils/audio"; import { arrayBufferToBase64, downSampleAudioFrame, floatTo16BitPCM, getMergedPCMData } from "./utils/audio";
import { headers, stompClient } from "./utils/ws";
let mqttData; let mqttData;
@ -21,7 +22,17 @@ const getAudio = () => {
// 存储一遍压缩后的音频 // 存储一遍压缩后的音频
buffers.push(downedFloat32Arr); buffers.push(downedFloat32Arr);
client.publish(mqttData.recognition_topic, sendItem); // client.publish(mqttData.recognition_topic, sendItem);
stompClient.publish({
destination: "/recognition/push",
binaryBody: new Uint8Array(floatTo16BitPCM(downedFloat32Arr)),
headers: {
...headers,
'content-type': 'application/octet-stream',
},
});
}); });
}; };

29
src/utils/ws.ts Normal file
View File

@ -0,0 +1,29 @@
import { Client } from "@stomp/stompjs";
export const headers = {
Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbl91c2VyX2tleSI6ImJjNzY3ZWMyLTUxNDktNDVlMS1iNjg5LWMyMGI3ZmE2MWExZiIsImlhdCI6MTcwNDg1NjczMywiZXhwIjoxNzA1NDYxNTMzfQ.EaMH-eyHm1T7GDbTQbitpeIER0wMXYN8PlzjkUhB93Y'
};
export const stompClient = new Client({
brokerURL: 'ws://127.0.0.1:8080/recognitionWs',
connectHeaders: headers
});
stompClient.activate();
stompClient.onConnect = (frame) => {
console.log('Connected: ' + frame);
// stompClient.subscribe('/recognitionTopic/c3fb0f9fd12f41dba48fe279b9cd7dc6/user/ww', (greeting) => {
// console.log(greeting);
// });
};
stompClient.onWebSocketError = (error) => {
console.error('Error with websocket', error);
};
stompClient.onStompError = (frame) => {
console.error('Broker reported error: ' + frame.headers['message']);
console.error('Additional details: ' + frame.body);
};