refactor: 重构sse消息推送,封装sseManger

This commit is contained in:
czhqwer
2025-03-30 14:03:42 +08:00
parent 1982a450a0
commit a38bfe0a69
9 changed files with 52 additions and 128 deletions

View File

@@ -51,6 +51,7 @@
</template>
<script>
import sseManager from '@/utils/sse';
import { getPassword, setPassword } from '@/utils/api';
export default {
@@ -64,10 +65,16 @@ export default {
tempPassword: '',
inputPassword: '',
showPasswordDialog: false,
mainUser: false,
};
},
mounted() {
this.getPassword();
sseManager.subscribe('setPassword', () => {
if (!this.mainUser) {
this.getPassword();
}
});
},
methods: {
async getPassword() {
@@ -76,7 +83,7 @@ export default {
const dataStr = res.data.toString();
const password = dataStr.slice(0, -1);
const mainUser = dataStr.slice(-1) === '1';
this.mainUser = mainUser;
this.tempPassword = password;
if (password) {
this.tempEnablePassword = true;

View File

@@ -68,7 +68,8 @@
</template>
<script>
import { getSharedFiles, enableShare, getShareStatus, shareAddress, unShareFile, subscribeSharedFiles, subscribeSystemEvent } from '@/utils/api';
import sseManager from '@/utils/sse'
import { getSharedFiles, enableShare, getShareStatus, shareAddress, unShareFile } from '@/utils/api';
export default {
name: 'ShareFile',
@@ -80,8 +81,6 @@ export default {
fileList: [],
enableShare: false,
isAdmin: false,
sharedFilesEventSource: null,
systemEventSource: null,
};
},
mounted() {
@@ -89,26 +88,22 @@ export default {
this.getShareStatus();
this.getShareAddress();
this.sharedFilesEventSource = subscribeSharedFiles(() => {
sseManager.subscribe('sharedFileUpdate', () => {
this.fetchFiles();
});
this.systemEventSource = subscribeSystemEvent((event) => {
if (event.type === 'enableShare') {
this.fileList = [];
this.enableShare = event.data.enable;
this.fetchFiles();
}
sseManager.subscribe('enableShare', (event) => {
this.fileList = [];
this.enableShare = event.enable;
this.fetchFiles();
});
},
beforeDestroy() {
if (this.sharedFilesEventSource) {
this.sharedFilesEventSource.close();
}
if (this.systemEventSource) {
this.systemEventSource.close();
}
sseManager.unsubscribe('sharedFileUpdate');
sseManager.unsubscribe('enableShare');
},
methods: {
async getShareAddress() {
try {

View File

@@ -1,4 +1,4 @@
import { http, baseUrl, httpExtra } from './http';
import { http, httpExtra } from './http';
/**
* 获取上传进度
@@ -271,60 +271,6 @@ const setPassword = (password) => {
return http.post("/config/setPassword", formData);
}
/**
* 订阅共享文件更新
* @param {Function} callback 文件更新的回调函数
* @returns {EventSource} 返回 EventSource 实例
*/
const subscribeSharedFiles = (callback) => {
const eventSource = new EventSource(`${baseUrl}/sse/subscribeSharedFiles`);
eventSource.addEventListener('sharedFileUpdate', (event) => {
callback(event.data);
});
eventSource.onerror = (error) => {
console.error("SSE连接异常:", error);
if (eventSource.readyState === EventSource.CLOSED) {
console.log("连接已关闭");
}
};
return eventSource;
};
/**
* 订阅系统事件更新
* @param {Function} callback 文件更新的回调函数
* @returns {EventSource} 返回 EventSource 实例
*/
const subscribeSystemEvent = (callback) => {
const eventSource = new EventSource(`${baseUrl}/sse/subscribeSystemEvent`);
eventSource.addEventListener('systemEvent', (event) => {
try {
const data = JSON.parse(event.data);
callback(data);
} catch (e) {
console.error('JSON解析失败:', e, '原始数据:', event.data);
}
});
eventSource.onmessage = (event) => {
console.log('[SSE] Received default message:', event.data);
};
eventSource.onerror = (error) => {
console.error("SSE连接异常:", error);
if (eventSource.readyState === EventSource.CLOSED) {
console.log("连接已关闭");
}
};
return eventSource;
}
export {
getUploadProgress,
createMultipartUpload,
@@ -345,7 +291,5 @@ export {
setPassword,
addSharedFile,
deleteFile,
subscribeSharedFiles,
subscribeSystemEvent,
httpExtra
};