js 同源跨标签广播通信

话不多说直接上代码…

//BrowserChannel.js

/**
 * 作者:superzuo
 * 时间:2023/03/03 11:18:08
 * 功能:同源跨标签广播通信
 */
/* 定义mode来判断和通信 */
const MODE = {
    /* 通过工具关闭 */
    "tools-closeWin":"tools-closeWin",
    /* iportal 登录成功 */
    "user-login":"user-login",
    /* 工具申请提交 */
    "tools-applyFor":"tools-applyFor"
}

class BrowserChannel {
    constructor(name,mark) {
        this.Channel = new BroadcastChannel(name);
        this.Channel.id = this.creatId(name);
        this.mark = mark;
        this.MODE = MODE;
        /* 创建列表保存监听者 */
        this.Channel.listeners = new Set();
        /* 内容初始化 */
        this.ints()
    }

    ints() {
        /* 我是当前标签打开者,需要首次广播 */
        this.sendMsg({
            type: 1,
            text: "我来了!"
        });
        window.addEventListener("unload", () => {
            this.sendMsg({
                type: 0,
                text: "我走了!"
            });
        })
        /* 监听广播数据 */
        this.Channel.addEventListener("message", e => {
            let msg = e.data.msg;
            /* 有新标签来了 */
            if (msg.type === 1) {
                this.sendMsg({
                    type: 2,
                    text: "我收到了!"
                });
                this.Channel.listeners.add({
                    id:e.data.id,
                    mark:e.data.mark
                });
            } else if (msg.type === 2) {
                this.Channel.listeners.add({
                    id:e.data.id,
                    mark:e.data.mark
                });
            } else if (msg.type === 0) {
                this.Channel.listeners.delete({
                    id:e.data.id,
                    mark:e.data.mark
                });
            }
        })
    }

    /* 创建标签id标识监听者 */
    creatId(name) {
        const key = `Iportal-Channel-${name}`
        let id = +localStorage.getItem(key);
        if (!id) {
            id = 0;
        }
        id++;
        localStorage.setItem(key, id.toString());
        return id;
    }

    sendMsg(msg) {
        this.Channel.postMessage({
            id: this.Channel.id,
            mark:this.mark,
            msg
        })
    }

}

export default BrowserChannel;
//在main.js 加载
import BrowserChannel from "@/utils/BroadcastChannel";


/* 同源跨标签广播通信,主要用于赋能工具通信 */
let $BrowserChannel = new BrowserChannel("MyTools","MyMark");
Vue.prototype.$BrowserChannel = $BrowserChannel;
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/* 监听窗口来关闭当前已打开窗口 */
$BrowserChannel.Channel.addEventListener("message",e=>{
    let msg = e.data.msg;
    /* 在未登录的情况下关闭当前iportal,然后重新打开 */
    if(!!msg.mode && msg.mode === $BrowserChannel.MODE["tools-closeWin"]){
        //相当于window.close()操作
        Utility.closeWinPage();
    }
})

//其他地方应用
this.$BrowserChannel.Channel.addEventListener("message", e => {
      let msg = e.data.msg;
      /* 监听用户登录 */
      if (!!msg.mode && msg.mode === this.$BrowserChannel.MODE['user-login']) {
        //登录了获取用户信息
        !!msg.success && this.getUserInfo();
      }
})

//发送广播消息
 this.$BrowserChannel.sendMsg({
     mode: this.$BrowserChannel.MODE['tools-applyFor']
 })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值