Update: WebSocket支持MicroPython板卡烧录固件及上传程序

This commit is contained in:
王立帮
2024-12-02 21:50:56 +08:00
parent 4008e1aab5
commit a5c1ef4269
14 changed files with 379 additions and 521 deletions

View File

@@ -11,14 +11,42 @@ class WebSocket {
this.#socket_ = io(path, option);
}
#detectStatus_(status, callback) {
window.setTimeout(() => {
if (status.finished) {
return;
}
if (this.isConnected()) {
this.#detectStatus_(status, callback);
} else {
callback({
error: 'socket is not connected'
});
status.finished = true;
}
}, 1000);
}
emit(eventName, ...args) {
const callback = args.pop();
if (this.isConnected()) {
return this.#socket_.emit(eventName, ...args);
} else {
const callback = args.pop();
callback({
error: new Error('socket is not connected')
let emitStatus = {
finished: false
};
let status = this.#socket_.emit(eventName, ...args, (...callbackArgs) => {
if (emitStatus.finished) {
return;
}
emitStatus.finished = true;
callback(...callbackArgs);
});
this.#detectStatus_(emitStatus, callback);
return status;
} else {
callback({
error: 'socket is not connected'
});
return false;
}
}