更新WebSocket实现

pull/76/head
rays 2021-06-05 18:26:29 +08:00
parent 6c0786c4aa
commit 606ea891ea
2 changed files with 16 additions and 19 deletions

View File

@ -51,8 +51,8 @@ layui.define(['jquery', 'layer'], function (exports) {
try {
// 发送心跳检测
let heartMsg = {
"type": "299999",
"data": new Date().getMilliseconds()
"clientMsgType": "299999",
"data": new Date().getTime()
};
_self.send(JSON.stringify(heartMsg))
} catch (e) {

View File

@ -45,11 +45,8 @@
<script type="text/javascript" src="${ctxPath}/assets/common/js/common.js?v=${constants.getReleaseVersion()}"></script>
<script>
// WebSocket实例
var wsInst = null
// 全局消息类型声明
const WEB_SOCKET_MSG_TYPE = {
window.WEB_SOCKET_MSG_TYPE = {
SERVER: {
SYS_NOTICE_MSG_TYPE: "100001"
},
@ -59,15 +56,15 @@
}
// 全局消息类型和回调函数维护中心
const msgTypeCallBackMap = new Map()
window.msgTypeCallBackMap = new Map()
// 新增一个回调
function insertCallback(msgType, func) {
window.insertCallback = (msgType, func) => {
msgTypeCallBackMap.set(msgType, func)
}
// 删除一个回调
function deleteCallback(msgType) {
window.deleteCallback = (msgType) => {
msgTypeCallBackMap.delete(msgType)
}
@ -128,47 +125,47 @@
});
// 全局WebSocket加载
wsInst = ws.render({
window.wsInst = ws.render({
wsUrl: Feng.wsUrl, //WebSocket的地址
connectErr: function (event) {
connectErr: (event) => {
console.log(event)
//如果不支持websocket 回调
},
onWsError: function (event) {
onWsError: (event) => {
//发生连接错误回调
},
onWsOpen: function (event) {
onWsOpen: (event) => {
// 绑定用户
let initMsg = {
"type": WEB_SOCKET_MSG_TYPE.CLIENT.USER_ADD_MSG_TYPE,
"clientMsgType": WEB_SOCKET_MSG_TYPE.CLIENT.USER_ADD_MSG_TYPE,
"formUserId": Feng.userId,
"data": WEB_SOCKET_MSG_TYPE.SERVER.SYS_NOTICE_MSG_TYPE
};
wsInst.send(JSON.stringify(initMsg));
},
onWsMessage: function (event) {
onWsMessage: (event) => {
//服务器发送消息回调
let data = event.data;
try {
let originalData = JSON.parse(data)
let func = msgTypeCallBackMap.get(originalData.type);
let func = msgTypeCallBackMap.get(originalData.serverMsgType);
if (func) {
eval(func(originalData));
}
} catch (e) {
}
},
onWsClose: function (event) {
onWsClose: (event) => {
//关闭连接回调
},
wsSend: function (event) {
wsSend: (event) => {
//发送成功后的回调
},
})
// 注册系统消息监听
insertCallback(WEB_SOCKET_MSG_TYPE.SERVER.SYS_NOTICE_MSG_TYPE, function (massage) {
window.insertCallback(window.WEB_SOCKET_MSG_TYPE.SERVER.SYS_NOTICE_MSG_TYPE, (massage) => {
notice.info({
title: '[' + massage.data.businessTypeValue + ']' + massage.data.messageTitle,
message: massage.data.messageContent,