import { setOriginalUrl } from './stateMap' export default class EventClient { private source : EventSource | undefined = undefined private readonly signinUrl: string constructor(signinUrl: string) { this.signinUrl = signinUrl this.source = new EventSource((import.meta.env.BASE_URL || '/') + 'sse') this.source.onmessage = (e: MessageEvent) => { this.handleMsg(JSON.parse(e.data)) } this.source.onerror = () => { this.source?.close() this.source = undefined console.warn('SSE 连接关闭或出错') } } close() { this.source?.close() this.source = undefined } handleMsg(data: any) { if(data.type === 'command' && data.value === 'Re-login') { // 跳转前,保存原始地址 const urlParam = new URLSearchParams(this.signinUrl) const state = urlParam.get('state') if (state) { setOriginalUrl(state, window.location.href) } // 重新登录,跳转到登录页面 window.location.href = this.signinUrl.replace(/^https?:\/\/[^/]+/, '') } } }