mirror of
https://github.com/OpenBMB/MiniCPM-V.git
synced 2026-02-05 18:29:18 +08:00
Update to MiniCPM-o 2.6
This commit is contained in:
44
web_demos/minicpm-o_2.6/web_server/src/utils/index.js
Normal file
44
web_demos/minicpm-o_2.6/web_server/src/utils/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
// 判断终端是pc还是移动端
|
||||
export const isMobile = () => {
|
||||
let flag = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Linux/i.test(navigator.userAgent);
|
||||
const platform = navigator.platform;
|
||||
// iPad上的Safari
|
||||
if (platform === 'MacIntel' && navigator.maxTouchPoints > 1) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
};
|
||||
// 单片语音长度(单位:ms)
|
||||
const voicePerLength = 200;
|
||||
|
||||
// 图片计数,算出在哪一次发送语音时,同时发送图片。例如一片语音100ms,一秒钟发送一次语音,即发送的第10片语音时需要带一张图片
|
||||
export const maxCount = 1000 / voicePerLength;
|
||||
|
||||
export const getChunkLength = sampleRate => {
|
||||
return sampleRate * (voicePerLength / 1000);
|
||||
};
|
||||
|
||||
export const isAvailablePort = port => {
|
||||
return [
|
||||
8000, 8001, 8002, 8003, 8004, 8010, 8011, 8012, 8013, 8014, 8020, 8021, 8022, 8023, 8024, 8025, 8026, 8027,
|
||||
8028, 32449
|
||||
].includes(port);
|
||||
};
|
||||
|
||||
// 文件转base64格式
|
||||
export const fileToBase64 = file => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!file) {
|
||||
reject('文件不能为空');
|
||||
}
|
||||
const reader = new FileReader();
|
||||
reader.onload = e => {
|
||||
const base64String = e.target.result;
|
||||
resolve(base64String);
|
||||
};
|
||||
reader.onerror = () => {
|
||||
reject('文件转码失败');
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
};
|
||||
91
web_demos/minicpm-o_2.6/web_server/src/utils/websocket.js
Normal file
91
web_demos/minicpm-o_2.6/web_server/src/utils/websocket.js
Normal file
@@ -0,0 +1,91 @@
|
||||
class WebSocketClient {
|
||||
constructor(url, maxReconnectAttempts = 5, reconnectInterval = 5000) {
|
||||
this.url = url;
|
||||
this.socket = null;
|
||||
this.eventHandlers = {};
|
||||
this.maxReconnectAttempts = maxReconnectAttempts;
|
||||
this.reconnectInterval = reconnectInterval;
|
||||
this.reconnectAttempts = 0;
|
||||
this.reconnectTimer = null;
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.reconnectAttempts = 0;
|
||||
this.establishConnection();
|
||||
}
|
||||
|
||||
establishConnection() {
|
||||
this.socket = new WebSocket(this.url);
|
||||
|
||||
this.socket.onopen = () => {
|
||||
console.log('WebSocket connection opened');
|
||||
this.reconnectAttempts = 0; // Reset reconnect attempts on successful connection
|
||||
this.emit('open');
|
||||
};
|
||||
|
||||
this.socket.onclose = event => {
|
||||
console.log('WebSocket connection closed', event);
|
||||
this.emit('close', event);
|
||||
// 1005为主动关闭websocket
|
||||
if (event.code !== 1005) {
|
||||
this.reconnect();
|
||||
}
|
||||
};
|
||||
|
||||
this.socket.onerror = error => {
|
||||
console.error('WebSocket error', error);
|
||||
this.emit('error', error);
|
||||
// Optionally, you may want to trigger a reconnect on error as well
|
||||
// this.reconnect();
|
||||
};
|
||||
|
||||
this.socket.onmessage = message => {
|
||||
// console.log('WebSocket message received', message.data);
|
||||
this.emit('message', message.data);
|
||||
};
|
||||
}
|
||||
|
||||
send(data) {
|
||||
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
||||
this.socket.send(data);
|
||||
} else {
|
||||
console.error('WebSocket is not open');
|
||||
}
|
||||
}
|
||||
|
||||
on(event, handler) {
|
||||
// if (!this.eventHandlers[event]) {
|
||||
this.eventHandlers[event] = [];
|
||||
// }
|
||||
this.eventHandlers[event].push(handler);
|
||||
// console.log('Event handler added:', this.eventHandlers, event);
|
||||
}
|
||||
|
||||
emit(event, ...args) {
|
||||
if (this.eventHandlers[event]) {
|
||||
this.eventHandlers[event].forEach(handler => handler(...args));
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
if (this.socket) {
|
||||
this.socket.close();
|
||||
}
|
||||
clearTimeout(this.reconnectTimer);
|
||||
}
|
||||
|
||||
reconnect() {
|
||||
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||
console.log(`Reconnecting attempt ${this.reconnectAttempts + 1}/${this.maxReconnectAttempts}...`);
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.reconnectAttempts++;
|
||||
this.establishConnection();
|
||||
}, this.reconnectInterval);
|
||||
} else {
|
||||
console.error('Max reconnect attempts reached. WebSocket will not attempt to reconnect.');
|
||||
this.emit('max-reconnect-attempts');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default WebSocketClient;
|
||||
Reference in New Issue
Block a user