当前位置: 首页 > news >正文

360推广 网站建设我是做推广的怎么找客户

360推广 网站建设,我是做推广的怎么找客户,湖南网站建设报价,网站文章排序前言 前段时间在做一个基于 psd 模板生成图片的应用,其中重要的功能就是打开编辑器页面来设计出图。但是有个问题,每当我点击一个模板,就会新开一个浏览器页签。现代浏览器是以空间换时间的运行思路来提高效率,这就导致了内存开销…

前言

前段时间在做一个基于 psd 模板生成图片的应用,其中重要的功能就是打开编辑器页面来设计出图。但是有个问题,每当我点击一个模板,就会新开一个浏览器页签。现代浏览器是以空间换时间的运行思路来提高效率,这就导致了内存开销会越来越大,也曾想过postmessage来解决这个问题,但是呢 postmessage 是跨域广播,说白了,我 post 的消息任意页签都能 listen 到,不友好。最近刷抖音时,看到了一个前端教学视频,其中就讲到了网页音乐单开的实现方式,核心原理:**BroadcastChannel**。

技术原理

BroadcastChannel 接口代理了一个命名频道,可以让指定 origin 下的任意 browsing context 来订阅它。它允许同源的不同浏览器窗口,Tab 页,frame 或者 iframe 下的不同文档之间相互通信。通过触发一个 message 事件,消息可以广播到所有监听了该频道的 BroadcastChannel 对象。

创建或者加入频道

客户端通过构造函数,传入频道名称即可创建或加入频道。如果当前不存在此命名的频道,就会初始化并创建。

// 创建或加入频道
const channel = new BroadcastChannel('editor_channel');

发送消息

基于刚才创建或加入的频道实例,调用 postMessage 方法发送消息。可以使用 BroadcastChannel.postMessage()  发送一条任意 Object 类型的消息,给所有同源下监听了该频道的所有浏览器上下文。消息以 message 事件的形式发送给每一个绑定到该频道的广播频道。

channel.postMessage(message: any);

接受消息

通过监听 message 事件即可接收到同频道发送的任意消息。

channel.addEventListener('message', ({data: any}) => {console.log(data);
})
// 或者
channel.onmessage = ({data: any}) => {console.log(data);
}

异常处理

通过监听 messageerror  事件即可捕获异常。

qh_channel.addEventListener('messageerror', e) => {console.error(e);
})
// 或者
qh_channel.onmessagerror = (e) => {console.error(e);
}

断开连接

调用 close() 方法即可断开对象和基础通道之间的链接。

qh_channel.close()

实现

原理搞清楚了,那么接下来就是实战了。 先上效果图:

e39882d469d7df7c8d6c897b2843653f.gif

图中使用了2个受控页面,目的是想验证多个页面能够被统一控制,而且咱也的确控制不了用户的某些操作。

封装 Channel

const editorList = [];
/*** @description: 页面通信类* @param {String} channelName channel名称* @param {String} page 实例化channel的页面名称* @param {Boolean} isEditor 是否是编辑器* @param {String} editorName 编辑器页面名称* @param {Function} onmessage 接收到消息的回调* @return {Channel} channel实例*/
export default class Channel {constructor({ channelName, page, isEditor = false, editorName, onmessage }) {if (!page) throw new Error('page is required');if (!isEditor && !editorName) throw new Error('editorName is required');this.__uuid__ = Math.random().toString(36).substr(2);this.isEditor = isEditor; // 是否是编辑器this.editorName = editorName; // 编辑器页面名称this.page = page; // 实例化channel的页面名称this.name = channelName ?? 'qh_channel'; // channel名称this.channel = new BroadcastChannel(this.name);this.addEvent(onmessage);this.load(); // 告诉其他页面,我初始化完了}addEvent(onmessage) {this.channel.onmessage = ({ data: { type, page, data, uuid } }) => {if (!this.isEditor) {if (page === this.editorName) {// 如果是编辑器页面发送的消息this.updateEditor(type, uuid);}} else if (type === 'load' && page !== this.page) {// 其他页面加载时,告诉知我已经存在了this.load();}if (onmessage) {onmessage({ type, page, data });}};}// 如果用户手动打开了多个编辑器,需要更新编辑器列表updateEditor(type, uuid) {const index = editorList.indexOf(uuid);if (type === 'load') {if (index === -1) {editorList.push(uuid);}} else if (type === 'unload') {if (index !== -1) {editorList.splice(index, 1);}}}postMessage(data) {if (!!editorList.length || this.isEditor) {const newData = { page: this.page, uuid: this.__uuid__, ...JSON.parse(JSON.stringify(data)) };this.channel.postMessage(newData);return true;}return false;}load() {this.channel.postMessage({ type: 'load', uuid: this.__uuid__, page: this.page });}unload() {this.channel.postMessage({ type: 'unload', uuid: this.__uuid__, page: this.page });this.channel.onmessage = null;this.channel.close();}close() {}
}

主控页面逻辑

在主控页面引入并实例化 Channel 类。其中 pageeditorName 必须填写。

import Channel from '@/utils/channel';const editorChannel = new Channel({page: 'template_index',editorName: 'editor_index',onmessage: ({ type, page, data }) => {if (type === 'confirm' && page === 'editor_index') {Modal.confirm({title: '警告',icon: createVNode(ExclamationCircleOutlined),content: '模板还未保存,确认替换?',onOk() {editorChannel.postMessage({ type: 'force_data', data });},});}},
});
window.onunload = () => {editorChannel.unload();
};
onUnmounted(() => {editorChannel.unload();
});// 点击事件
const itemClick = (node) => {if (!editorChannel.postMessage({ type: 'data', data: node })) {const rt = router.resolve(`/editor/${node.id}`);safeOpen(rt.href);}
};

前边提到了 safeOpen ,实现方案也很简单,具体为什么要 safeOpen ,请自行 google

window.open(url, target, 'noreferrer,noopener');

受控页面逻辑

受控页面同样引入并实例化 Channel 类。page 必须填写并且要标记 isEditor: true ,明确告知这是编辑器页面,这样 Channel 类就知道在 onmessage 时知道如何处理逻辑了。

import Channel from '@/utils/channel';const editorChannel = new Channel({page: 'editor_index',isEditor: true,onmessage: ({ type, data }) => {if (type === 'data') {if (dirty) {// 有修改,在主控页面弹窗提醒editorChannel.postMessage({ type: 'confirm', data });} else {// 无修改window.location.href = `/editor/${data.id}`;}} else if (type === 'force_data') {// 强制更新数据window.location.href = `/editor/${data.id}`;}},
});
window.onunload = () => {editorChannel.unload();
};
onUnmounted(() => {editorChannel.unload();
});

其实呢代码量也不多,核心就是 Channel 类,记录编辑器广播通信的 __uuid__ (这里之所以使用数组记录是因为用户的确可以通过 url 地址强制多开,那咱也没办法,只能一并记录更新),当主控页面想要打开新的编辑器时,优先调用 postMessage 方法,根据其返回结果判断编辑器是否存在,如果存在就触发受控页面更新路由并加载,如果不存在就打开新页签。

总结

BroadcastChannel 是一个非常简单的 API ,内部包含了跨上下文同源通信的接口。它没有定义消息传输协议,故不同上下文中的不同文档需要自己实现。目前来看兼容性方面也基本没有问题。087af2f89703d90380bc08956c355e61.png

- END -

关于奇舞团

奇舞团是 360 集团最大的大前端团队,代表集团参与 W3C 和 ECMA 会员(TC39)工作。奇舞团非常重视人才培养,有工程师、讲师、翻译官、业务接口人、团队 Leader 等多种发展方向供员工选择,并辅以提供相应的技术力、专业力、通用力、领导力等培训课程。奇舞团以开放和求贤的心态欢迎各种优秀人才关注和加入奇舞团。

fd52afc6b5b4bd16b7c4617c658fa0f9.png

http://www.shuangfujiaoyu.com/news/62271.html

相关文章:

  • 网站重做 影响互联网广告销售好做吗
  • 基于微信小程序的毕业设计题目seo工具优化软件
  • 焦作市建设委员会网站免费产品推广软件
  • 江苏网站建设机构合肥百度seo代理
  • 企业网站经典案例网站运营一个月多少钱
  • 丹东网站制作厦门谷歌seo公司
  • 网站建设怎么销售百度一下官网首页百度
  • 西安流行病毒最新消息抚顺优化seo
  • 备案号 不放在网站首页淘宝直通车推广怎么收费
  • 做视频赚钱的网站怎么注册自己的网站
  • 阿里云镜像双wordpressseo优化网站教程
  • 番禺网站制作费用腾讯广告投放推广平台价格
  • 做网站视频网络优化工程师主要做什么
  • 容桂网站制作值得信赖湖北荆门今日头条
  • 建设产品网站课程开网店
  • 德清网站建设如何把一个关键词优化到首页
  • 网站流量高iis如何做负载均衡淘宝大数据查询平台
  • 鞍山云疫情最新windows10优化工具
  • 北京营销型网站建设培训管理课程培训
  • 西安建网站微信如何引流推广精准加人
  • 营口网站建设求职简历武汉大学人民医院精神科
  • 微商软件商城总站百度seo价格
  • 德州网站建设费用广东深圳疫情最新消息今天
  • 找人做效果图去什么网站百度联盟项目看广告挣钱
  • 在线视频网站a做免费下载关键词首页排名优化价格
  • 怎么网上接网站开发单自己做上海优化公司排行榜
  • 怎么能自己做网站企业网站推广的方法
  • 网站视频播放器用什么做的网站关键词优化多少钱
  • 代理彩票网站做链接国外网络推广
  • 创造自己的网站seo优化网络公司排名