A very simple window communication library.
npm install @haiyaotec/window-channel
Client
import {WindowChannel} from '@haiyaotec/window-channel'
let client = WindowChannel.newChannelClient(window,iFrame.contentWindow,"*")
function f() {
client.request('/hello', '客户端发送的消息', 1000)
.then((value) => {
console.log(value)
})
.catch((err) => {
console.log(err)
})
}
setInterval(f,3000)
Server
import {WindowChannel} from '@haiyaotec/window-channel'
let service=WindowChannel.newChannelService(window)
service.listen('/hello',(value)=>{
console.log(value)
return '服务端发送的消息'
})
service.observe('/dingyue',()=>{
console.log('订阅成功')
})
setTimeout(()=>{
service.broadcast('/dingyue','聊天室广播内容')
},20000)