From ea6628172b45b9f79fb615e0f52fccd2f87caad5 Mon Sep 17 00:00:00 2001 From: ch Date: Wed, 27 Apr 2022 10:52:59 +0800 Subject: [PATCH] socket --- common/utils/index.js | 8 +- common/utils/requset.js | 6 +- common/utils/websocket.js | 190 ++++++++++++++++++++------------------ main.js | 35 +++---- pages/seckill.vue | 3 +- 5 files changed, 121 insertions(+), 121 deletions(-) diff --git a/common/utils/index.js b/common/utils/index.js index b4f8fbc..b49d528 100644 --- a/common/utils/index.js +++ b/common/utils/index.js @@ -2,15 +2,15 @@ * @Author: ch * @Date: 2022-03-22 16:52:28 * @LastEditors: ch - * @LastEditTime: 2022-04-26 17:52:05 + * @LastEditTime: 2022-04-26 19:04:27 * @Description: 所有工具类统一在这输出 */ import * as util from './utils'; import * as requset from './requset'; -// import * as websocket from './requset'; +import * as websocket from './requset'; export * from './utils'; export * from './requset'; -// export * from './websocket'; +export * from './websocket'; -export default { ...util, ...requset} \ No newline at end of file +export default { ...util, ...requset, ...websocket} \ No newline at end of file diff --git a/common/utils/requset.js b/common/utils/requset.js index 7a70059..4eba271 100644 --- a/common/utils/requset.js +++ b/common/utils/requset.js @@ -2,16 +2,16 @@ * @Author: ch * @Date: 2022-03-17 17:42:32 * @LastEditors: ch - * @LastEditTime: 2022-04-26 18:13:01 + * @LastEditTime: 2022-04-26 18:35:18 * @Description: 项目接口请求统一处理器,返回一个需要token和不需要token的请求封装方法 */ import MsbUniRequest from '@/common/plugins/msbUniRequest'; import $store from '@/common/store'; -const ENV = 'prod'; +const ENV = 'test'; const BASE_URL = { - 'test' : 'https://like-app.mashibing.com/test', + 'test' : 'https://k8s-horse-gateway.mashibing.cn', // 'test' : '', 'release' : '', 'prod' : '//like-gateway.mashibing.com' diff --git a/common/utils/websocket.js b/common/utils/websocket.js index 9aa069d..3a2ad0b 100644 --- a/common/utils/websocket.js +++ b/common/utils/websocket.js @@ -2,105 +2,113 @@ * @Author: ch * @Date: 2022-04-25 14:39:19 * @LastEditors: ch - * @LastEditTime: 2022-04-26 16:28:01 + * @LastEditTime: 2022-04-27 10:12:40 * @Description: file content */ import store from "../store"; import {createUUID, formatDate} from '@/common/utils'; -export const MsbWebSkt = uni.connectSocket({ - url : `ws://192.168.10.92:8090/ws?client=${store.state.token}`, // url是websocket连接ip - success: () => { - console.log('websocket连接成功!') - }, - fail: e => { - console.log('连接失败' + e) - } -}); -MsbWebSkt.onClose(()=>{ - console.log('WebSocket关闭...!') -}) let sessionListFlag = false; -MsbWebSkt.onMessage((res)=>{ - const data = JSON.parse(res.data || {}); - if(data.code === 200){ - const ctx = data.content; - switch(data.traceType){ - // 会话列表 - case 1 : - sessionListFlag = true; - // 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框 - ctx.sessionVOS.forEach(item => { - item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') - item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); - let historyData = store.state.sessionData; - let hisIndex = historyData.findIndex(i => i.id === item.id); - if(hisIndex >= 0){ - historyData[hisIndex].lastMessage = item.lastMessage; - historyData[hisIndex].unreadCount++; - store.commit('SET_SESSION_DATA', historyData); - }else{ - item.messageList = []; - store.commit('SET_SESSION_DATA',[...historyData, item]); - } - }); - // 消息总数 - store.commit('SET_SESSION_MSG_COUNT', ctx.totalUnreadCount || 10); - break; - // 历史消息 - case 2 : - let newData = store.state.sessionData; - const hisIdx = newData.findIndex(i => i.id === ctx[0].sessionId); - ctx.forEach(item => { - item.createTimeStamp = formatDate(item.createTimeStamp, 'mm-dd hh:ii') - item.payload = JSON.parse(item.payload) - }) - newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList); - store.commit('SET_SESSION_DATA', newData); - console.log(newData); - - break; - // 会话消息 - case 7 : - // 没接到会话列表时接到的消息不做处理 - if(!sessionListFlag){ - break; - } - // ctx.forEach(item => { - ctx.payload = JSON.parse(ctx.payload || {}); - let historyData = store.state.sessionData; - const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId); - store.commit('SET_SESSION_MSG_COUNT', store.state.sessionMsgCount + 1); - if(hisIndex >= 0){ - // 存在会话往现有会话增加一条消息 - const curHisData = historyData[hisIndex]; - curHisData.messageList = [ ...(curHisData.messageList || []), ctx] - curHisData.unreadCount++; - store.commit('SET_SESSION_DATA', historyData); +const sessionList = (ctx) => { + sessionListFlag = true; + // 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框 + ctx.sessionVOS.forEach(item => { + item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii') + item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {}); + let historyData = store.state.sessionData; + let hisIndex = historyData.findIndex(i => i.id === item.id); + if(hisIndex >= 0){ + historyData[hisIndex].lastMessage = item.lastMessage; + historyData[hisIndex].unreadCount++; + store.commit('SET_SESSION_DATA', historyData); + }else{ + item.messageList = []; + store.commit('SET_SESSION_DATA',[...historyData, item]); + } + }); + // 消息总数 + store.commit('SET_SESSION_MSG_COUNT', ctx.totalUnreadCount || 10); +} +const historyMsg = (ctx) => { + let newData = store.state.sessionData; + const hisIdx = newData.findIndex(i => i.id === ctx[0].sessionId); + ctx.forEach(item => { + item.createTimeStamp = formatDate(item.createTimeStamp, 'mm-dd hh:ii') + item.payload = JSON.parse(item.payload) + }) + newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList); + store.commit('SET_SESSION_DATA', newData); +} - }else{ - // 会话列表不存在,则创建一个会话 - store.commit('SET_SESSION_DATA',[...historyData, { - fromAvatar : ctx.fromAvatar, - fromId : ctx.fromId, - fromNickname : ctx.fromNickname, - id : ctx.id, - lastMessage : ctx, - messageList : [ctx], - unreadCount : 1 - }]); - } - - // }); - break; - default : - break; +const sessionMsg = (ctx)=>{ + // 没接到会话列表时接到的消息不做处理 + if(!sessionListFlag){ + return + } +// ctx.forEach(item => { + ctx.payload = JSON.parse(ctx.payload || {}); + let historyData = store.state.sessionData; + const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId); + store.commit('SET_SESSION_MSG_COUNT', store.state.sessionMsgCount + 1); + if(hisIndex >= 0){ + // 存在会话往现有会话增加一条消息 + const curHisData = historyData[hisIndex]; + curHisData.messageList = [ ...(curHisData.messageList || []), ctx] + curHisData.unreadCount++; + store.commit('SET_SESSION_DATA', historyData); - } + }else{ + // 会话列表不存在,则创建一个会话 + store.commit('SET_SESSION_DATA',[...historyData, { + fromAvatar : ctx.fromAvatar, + fromId : ctx.fromId, + fromNickname : ctx.fromNickname, + id : ctx.id, + lastMessage : ctx, + messageList : [ctx], + unreadCount : 1 + }]); } -}) + +// }); +} +export let MsbWebSkt = null; -MsbWebSkt.onError(()=>{ - console.log('WebSocket连接错误') -}) +export const MsbWebSktInit = () => { + return new Promise((resolve, reject) => { + MsbWebSkt = uni.connectSocket({ + url : `ws://192.168.10.92:8090/ws?client=${store.state.token}`, // url是websocket连接ip + fail: e => { + reject(e) + } + }); + MsbWebSkt.onOpen(()=>{ + MsbWebSkt.onMessage((res)=>{ + const data = JSON.parse(res.data || {}); + if(data.code === 200){ + const ctx = data.content; + switch(data.traceType){ + // 会话列表 + case 1 : + sessionList(ctx); + break; + // 历史消息 + case 2 : + historyMsg(ctx) + + break; + // 会话消息 + case 7 : + sessionMsg(ctx) + break; + default : + break; + } + } + }) + resolve(); + }) + }) + +} diff --git a/main.js b/main.js index 862bc9a..e99cfee 100644 --- a/main.js +++ b/main.js @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2021-07-26 23:22:16 * @LastEditors: ch - * @LastEditTime: 2022-04-26 17:45:58 + * @LastEditTime: 2022-04-27 10:12:54 * @Description: file content */ import Vue from 'vue'; @@ -12,28 +12,21 @@ import {router,RouterMount} from '@/common/router/index.js'; import uView from 'uview-ui'; import store from '@/common/store'; import Confirm from '@/components/mount/index'; -// import {MsbWebSkt, createUUID} from '@/common/utils'; +import {MsbWebSkt, MsbWebSktInit, createUUID} from '@/common/utils'; +if(store.state.token){ // 进入应用则向IM发起心跳包 以及获取IM会话数据 -// MsbWebSkt.onOpen(()=>{ -// MsbWebSkt.send({ -// data : JSON.stringify({ -// traceId : createUUID(), -// traceType : "1", -// content: { sysId : "1"} -// }) -// }); -// setInterval(()=>{ -// MsbWebSkt.send({ -// data : JSON.stringify({ -// traceId : createUUID(), -// traceType : "0", -// content: { text : "ping"} -// }) -// }) - -// }, 5000) -// }); + MsbWebSktInit().then(()=>{ + MsbWebSkt.send({ + data : JSON.stringify({ + traceId : createUUID(), + traceType : "1", + content: { sysId : "1"} + }) + }); + }) +} + Vue.use(router); diff --git a/pages/seckill.vue b/pages/seckill.vue index a8f8d72..0669e03 100644 --- a/pages/seckill.vue +++ b/pages/seckill.vue @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-03-21 18:08:07 * @LastEditors: ch - * @LastEditTime: 2022-04-25 10:43:11 + * @LastEditTime: 2022-04-27 10:47:46 * @Description: file content -->