Commit aa728c17 authored by martin hou's avatar martin hou

fix: 增加对无效文件传输时的异常处理

parent e87b778c
...@@ -847,6 +847,18 @@ export function Home() { ...@@ -847,6 +847,18 @@ export function Home() {
await releaseJensen(); await releaseJensen();
} }
const handleTransferX = async () => {
let jensen = await getJensen();
if (jensen == null) return;
let rst = await jensen.getFile('aabccd.txt', 100, (data) => {
console.log('data', data.length);
}, (size) => {
console.log('size', size);
});
Logger.info('jensen', 'transfer-x', 'Transfer X: ' + JSON.stringify(rst));
await releaseJensen();
}
return ( return (
<> <>
<div className="btn-container" style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center', flexWrap: 'wrap' }}> <div className="btn-container" style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center', flexWrap: 'wrap' }}>
...@@ -893,6 +905,7 @@ export function Home() { ...@@ -893,6 +905,7 @@ export function Home() {
<button onClick={getAudioInputDevice}>Get Audio Input Device</button> <button onClick={getAudioInputDevice}>Get Audio Input Device</button>
<button onClick={switchAudioInputDevice}>Switch Audio Input Device</button> <button onClick={switchAudioInputDevice}>Switch Audio Input Device</button>
<button onClick={handleReadBlock}>Read Block</button> <button onClick={handleReadBlock}>Read Block</button>
<button onClick={handleTransferX}>Transer Filex</button>
<span style={{ marginLeft: '8px' }}>Interval(ms): </span> <span style={{ marginLeft: '8px' }}>Interval(ms): </span>
<input <input
......
...@@ -1164,7 +1164,13 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr ...@@ -1164,7 +1164,13 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr
let flen = 0; let flen = 0;
let handler = (msg) => { let handler = (msg) => {
if (msg != null) { if (msg != null) {
flen += msg.body.length || msg.body.byteLength; const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 0;
if (bodyLen === 0 && flen === 0) {
Logger.info('jensen', 'getFilePart', 'file not found (0-length body).');
ondata('fail');
return 'fail';
}
flen += bodyLen;
ondata(msg.body); ondata(msg.body);
Logger.info('jensen', 'getFilePart length', `${length} ${flen}`); Logger.info('jensen', 'getFilePart length', `${length} ${flen}`);
if (flen >= length) { if (flen >= length) {
...@@ -1174,6 +1180,7 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr ...@@ -1174,6 +1180,7 @@ Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogr
} else { } else {
Logger.info('jensen', 'getFilePart', 'file download fail.'); Logger.info('jensen', 'getFilePart', 'file download fail.');
ondata('fail'); ondata('fail');
return 'fail';
} }
}; };
this.onreceive = onprogress; this.onreceive = onprogress;
...@@ -1236,7 +1243,13 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -1236,7 +1243,13 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
let flen = 0; let flen = 0;
let handler = (msg) => { let handler = (msg) => {
if (msg != null) { if (msg != null) {
flen += msg.body.length || msg.body.byteLength; const bodyLen = msg.body ? (msg.body.byteLength ?? msg.body.length ?? 0) : 0;
if (bodyLen === 0 && flen === 0) {
Logger.info('jensen', 'getFile', 'file not found (0-length body).');
ondata('fail');
return 'fail';
}
flen += bodyLen;
ondata(msg.body); ondata(msg.body);
// Logger.info('jensen', 'getFile length', `${length} ${flen}`); // Logger.info('jensen', 'getFile length', `${length} ${flen}`);
if (flen >= length) { if (flen >= length) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment