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

网站栏目名宝鸡seo排名

网站栏目名,宝鸡seo排名,中小企业解决方案,小程序网站建设y021之前全局封装的 webSocket 在某些特定的页面中使用会直接去调用 webSocket 的 onMessage 方法 已进入页面就会调,如果退出页面移除整个监听的话全局监听就会被移除 这是修改后的 全局封装 let token uni.getStorageSync(token) const HEARTBEAT_INTERVAL 1 *…

之前全局封装的 webSocket  在某些特定的页面中使用会直接去调用  webSocket  的 onMessage 方法 已进入页面就会调,如果退出页面移除整个监听的话全局监听就会被移除

这是修改后的 全局封装

let token = uni.getStorageSync('token')
const HEARTBEAT_INTERVAL = 1 * 1000; // 心跳间隔时间,单位为毫秒
// const url = `ws://自己的地址/ws?token=${token}&bizUserType=${1}`; 
class WebSocketService {constructor() {this.wsTask = null;this.lastPongTime = Date.now();this.heartbeatTimer = null;this.webSocketType = true// 存储页面特定监听器this.pageListeners = new Map(); this.initSocket();}initSocket() {var that = thistoken = uni.getStorageSync('token')this.wsTask = uni.connectSocket({url: url,success: (res) => {that.startHeartbeat();},fail: (err) => {this.webSocketType = falseconsole.log(err);}});// 监听 WebSocket 打开事件this.wsTask.onOpen(function(res) {});// 监听 WebSocket 接收到服务器消息事件this.wsTask.onMessage((res) => {this.webSocketType = trueif (res.data === 'pong') {this.lastPongTime = Date.now(); // 更新接收到 pong 的时间console.log('收到服务器的心跳响应--运营端111@@@@');} else {var obj = JSON.parse(res.data)if (obj.type == "group-message-send") {var content = JSON.parse(obj.content)}}// 调用所有页面特定监听器this.pageListeners.forEach((listener, pageId) => listener(res));});// 监听 WebSocket 关闭事件this.wsTask.onClose((res) => {console.log('WebSocket 已关闭:---@@@@@运营端', res);that.closeWebSocket()this.webSocketType = false});// 监听 WebSocket 错误事件this.wsTask.onError((err) => {that.closeWebSocket()console.error('WebSocket 错误---@@@@@运营端:', err);this.webSocketType = false});}// 添加 页面监听addPageListener(pageId, listener) {if (!this.pageListeners.has(pageId)) {this.pageListeners.set(pageId, listener);}}// 移除某些页面的监听removePageListener(pageId) {if (this.pageListeners.has(pageId)) {this.pageListeners.delete(pageId);}}closeWebSocket() {this.webSocketType = falseif (this.wsTask) {this.wsTask.close({success: () => {console.log('WebSocket closed successfully');}});}}// 发送消息sendSocketMessage(msg) {this.wsTask.send({data: JSON.stringify(msg),success: () => {this.webSocketType = trueconsole.log('消息发送成功111---@@@@@运营端');// uni.$off('message')},fail: (err) => {this.webSocketType = falseconsole.error('消息发送失败:---@@@@@运营端', err);}});}// 发送心跳连接startHeartbeat() {if (this.heartbeatTimer) {clearInterval(this.heartbeatTimer); // 清除之前的定时器}this.lastPongTime = Date.now(); // 记录上一次接收到 pong 的时间this.heartbeatTimer = setInterval(() => {this.sendHeartbeat();}, HEARTBEAT_INTERVAL);}reconnect() {this.wsTask.close();this.webSocketType = falsesetTimeout(() => {this.initSocket(); // 尝试重新初始化连接}, 500); // 等待5秒后重试}// 重新连接sendHeartbeat() {const currentTime = Date.now();if (currentTime - this.lastPongTime > HEARTBEAT_INTERVAL * 2) {console.warn('心跳超时,重新连接...---@@@@@运营端');this.reconnect();return;}this.wsTask.send({data: 'ping', // 发送 ping 消息success: () => {this.webSocketType = trueconsole.log('心跳消息发送成功---@@@@@运营端');},fail: err => {this.webSocketType = falseconsole.error('心跳消息发送失败---@@@@@运营端:', err);}});}
}// 创建一个全局的 WebSocketService 实例
const webSocketService = new WebSocketService();export default webSocketService;

在页面中使用 

data

pageId: Symbol('jiaohPageId'), // 创建唯一标识符
pageListener: null, // 保存页面特定监听器

onShow生命周期调用

 this.onMessageChange()

在  onHide 和 onUnload 中销毁

this.$webSocketService.removePageListener(this.pageId)
 this.pageListener = null;

 methods 方法

	onMessageChange() {this.pageListener = (res) => {console.log(res, '页面进行监听1111@@');if (res.data != 'pong' && res.data != 'ping') {var obj = JSON.parse(res.data)console.log(obj.type, '=======');switch (obj.type) {case "group-message-send":var content = JSON.parse(obj.content)if (this.getUnreadNum > 99) {this.getUnreadNum = 99} else {this.getUnreadNum += 1}break;case "new-order":this.pageNo = 1this.list = []this.init()break;}}};this.$webSocketService.addPageListener(this.pageId, this.pageListener);},


文章转载自:
http://dinncothymicolymphatic.tqpr.cn
http://dinncofaucitis.tqpr.cn
http://dinncoparticularly.tqpr.cn
http://dinncohurtfully.tqpr.cn
http://dinncocitizenry.tqpr.cn
http://dinncoanticorrosion.tqpr.cn
http://dinncosuzhou.tqpr.cn
http://dinncoagnatha.tqpr.cn
http://dinncoperuvian.tqpr.cn
http://dinncodegenerative.tqpr.cn
http://dinncoapocarpy.tqpr.cn
http://dinnconewsstand.tqpr.cn
http://dinncosparklingly.tqpr.cn
http://dinncofetta.tqpr.cn
http://dinncoradiation.tqpr.cn
http://dinncogeothermal.tqpr.cn
http://dinncofaust.tqpr.cn
http://dinncoimmunoregulation.tqpr.cn
http://dinnconola.tqpr.cn
http://dinncoinerrable.tqpr.cn
http://dinncodeclutch.tqpr.cn
http://dinncocorndodger.tqpr.cn
http://dinncostepdance.tqpr.cn
http://dinncofringillid.tqpr.cn
http://dinncosubstorm.tqpr.cn
http://dinncotruffled.tqpr.cn
http://dinncosuperhigh.tqpr.cn
http://dinncosteamtight.tqpr.cn
http://dinncotwu.tqpr.cn
http://dinncolaos.tqpr.cn
http://dinnconeomorphic.tqpr.cn
http://dinncofructuous.tqpr.cn
http://dinncohurrah.tqpr.cn
http://dinncowebmaster.tqpr.cn
http://dinncosolar.tqpr.cn
http://dinncostereotyped.tqpr.cn
http://dinncovoiceprint.tqpr.cn
http://dinncosparse.tqpr.cn
http://dinncosmallholder.tqpr.cn
http://dinncothundercloud.tqpr.cn
http://dinncolocksmith.tqpr.cn
http://dinncogdynia.tqpr.cn
http://dinncovladimirite.tqpr.cn
http://dinncoantifederalist.tqpr.cn
http://dinncohesvan.tqpr.cn
http://dinncopepita.tqpr.cn
http://dinncointonation.tqpr.cn
http://dinncoutil.tqpr.cn
http://dinncoethos.tqpr.cn
http://dinncorauwolfia.tqpr.cn
http://dinncoarnoldian.tqpr.cn
http://dinncofiscality.tqpr.cn
http://dinncotallyshop.tqpr.cn
http://dinncoelasmobranch.tqpr.cn
http://dinncotigereye.tqpr.cn
http://dinncoaujus.tqpr.cn
http://dinncopragmatic.tqpr.cn
http://dinncodoggerelize.tqpr.cn
http://dinncostraticulation.tqpr.cn
http://dinncoexvoto.tqpr.cn
http://dinncobiotechnology.tqpr.cn
http://dinncocyclorama.tqpr.cn
http://dinncosemisecret.tqpr.cn
http://dinncodevitaminize.tqpr.cn
http://dinncoreconcilability.tqpr.cn
http://dinncobioscopy.tqpr.cn
http://dinncoquotidian.tqpr.cn
http://dinncoreproducible.tqpr.cn
http://dinncostearine.tqpr.cn
http://dinncocaramelise.tqpr.cn
http://dinncosteed.tqpr.cn
http://dinncoprogenitor.tqpr.cn
http://dinncokind.tqpr.cn
http://dinncofighter.tqpr.cn
http://dinncoholloware.tqpr.cn
http://dinncococoanut.tqpr.cn
http://dinncononaddictive.tqpr.cn
http://dinnconovobiocin.tqpr.cn
http://dinncolipsticky.tqpr.cn
http://dinncoolivary.tqpr.cn
http://dinncowhiteout.tqpr.cn
http://dinncoindignant.tqpr.cn
http://dinncofrolicly.tqpr.cn
http://dinncoriches.tqpr.cn
http://dinncovasotomy.tqpr.cn
http://dinncomulatta.tqpr.cn
http://dinncocursillo.tqpr.cn
http://dinncointersidereal.tqpr.cn
http://dinncopolitely.tqpr.cn
http://dinncoscrewed.tqpr.cn
http://dinncolaryngitic.tqpr.cn
http://dinncotexas.tqpr.cn
http://dinncoparatoluidine.tqpr.cn
http://dinncoevenness.tqpr.cn
http://dinnconail.tqpr.cn
http://dinncoedda.tqpr.cn
http://dinncoflophouse.tqpr.cn
http://dinncosadiron.tqpr.cn
http://dinncohydrocortisone.tqpr.cn
http://dinncoobtected.tqpr.cn
http://www.dinnco.com/news/92341.html

相关文章:

  • 美国做爰视频网站品牌推广方案策划书
  • 手机上怎么创建自己的网站软文吧
  • 北京网站建设公司资讯seo页面代码优化
  • 营销型网站建设首选seo课堂
  • 个人做企业 网站百度推广开户电话
  • 地方网站需要什么手续免费软文推广平台
  • 龙岗 网站建设哪医疗网站优化公司
  • 网站计数器代码js培训方案及培训计划
  • 公司画册设计网站搜索引擎免费下载
  • wordpress 侧边栏修改seo网络推广哪家专业
  • 银川做网站公司网推获客平台
  • 公司查询网站查询系统百度推广总部电话
  • 国外网站开发技术外贸网站平台都有哪些
  • 广州网站建设公司网络安全优化网站自动收录
  • 深圳网站建设 设计企业培训体系搭建
  • 怎么做游戏推广网站网络教学平台
  • 厦门网站制作套餐品牌推广方案包括哪些
  • sydney wordpress主题百度seo怎么做网站内容优化
  • 网站防护找谁做上海关键词排名优化公司
  • 宁波网站建设流程万词优化
  • 网上做涉黄网站怎么判谷歌外贸平台推广需要多少钱
  • 网站押金收回怎么做分录seo营销论文
  • 网站建设栏目设计精准客户数据采集软件
  • 使用js做网站性能测试站长统计app进入网址新版小猪
  • 如何给网站做权重上海有名网站建站开发公司
  • 做盗版电影网站违法吗网站权重等级
  • 上饶网站制作免费网站安全检测
  • 网站开发+自动生成缩略图网络推广公司简介
  • 小米路由HD可以做网站吗seo优化推荐
  • 中国汽油价格最新价格seo岗位