mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-22 17:26:54 +08:00
feat(web): 添加 iframe 通信支持
为应用添加 iframe 嵌入功能的通信支持
This commit is contained in:
26
web/utils/message-channel.ts
Normal file
26
web/utils/message-channel.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// 定义一个全局的 MessageChannel 用于 iframe 通信
|
||||
let port
|
||||
|
||||
export function activeMessageChannel() {
|
||||
// 只监听一次初始化消息,接收到后自动移除监听器
|
||||
window.addEventListener('message', function initHandler(event) {
|
||||
console.log('尝试接收初始化消息:', event.data)
|
||||
|
||||
console.log('接收到初始化消息,设置端口')
|
||||
port = event.ports[0]
|
||||
|
||||
// 设置端口消息处理函数
|
||||
port.onmessage = (msgEvent) => {
|
||||
console.log('接收到worker消息:', msgEvent.data)
|
||||
}
|
||||
|
||||
Object.defineProperty(globalThis, 'port', {
|
||||
value: port,
|
||||
})
|
||||
|
||||
// 初始化完成后移除这个事件监听器
|
||||
window.removeEventListener('message', initHandler)
|
||||
}, /* {
|
||||
once: true,
|
||||
} */)
|
||||
}
|
||||
Reference in New Issue
Block a user