|
|
|
@ -37,14 +37,40 @@
|
|
|
|
|
var Feng = {
|
|
|
|
|
ctxPath: "${ctxPath}",
|
|
|
|
|
version: '${constants.getReleaseVersion()}',
|
|
|
|
|
wsUrl: '${wsUrl}'
|
|
|
|
|
wsUrl: '${wsUrl}',
|
|
|
|
|
userId: '${userId}'
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
<script type="text/javascript" src="${ctxPath}/assets/common/libs/layui/layui.js?v=${constants.getReleaseVersion()}"></script>
|
|
|
|
|
<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 = {
|
|
|
|
|
SERVER: {
|
|
|
|
|
SYS_NOTICE_MSG_TYPE: "100001"
|
|
|
|
|
},
|
|
|
|
|
CLIENT: {
|
|
|
|
|
USER_ADD_MSG_TYPE: "200001"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 全局消息类型和回调函数维护中心
|
|
|
|
|
const msgTypeCallBackMap = new Map()
|
|
|
|
|
|
|
|
|
|
// 新增一个回调
|
|
|
|
|
function insertCallback(msgType, func) {
|
|
|
|
|
msgTypeCallBackMap.set(msgType, func)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除一个回调
|
|
|
|
|
function deleteCallback(msgType) {
|
|
|
|
|
msgTypeCallBackMap.delete(msgType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layui.use(['layer', 'element', 'admin', 'index', 'HttpRequest', 'ws', 'notice', 'i18n'], function () {
|
|
|
|
|
var $ = layui.jquery;
|
|
|
|
|
var layer = layui.layer;
|
|
|
|
@ -100,6 +126,8 @@
|
|
|
|
|
});
|
|
|
|
|
request.start();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 全局WebSocket加载
|
|
|
|
|
wsInst = ws.render({
|
|
|
|
|
wsUrl: Feng.wsUrl, //WebSocket的地址
|
|
|
|
|
connectErr: function (event) {
|
|
|
|
@ -110,21 +138,23 @@
|
|
|
|
|
//发生连接错误回调
|
|
|
|
|
},
|
|
|
|
|
onWsOpen: function (event) {
|
|
|
|
|
//连接成功回调
|
|
|
|
|
console.log("Socket 已打开");
|
|
|
|
|
wsInst.send("消息发送测试(From Client)");
|
|
|
|
|
// 绑定用户
|
|
|
|
|
let initMsg = {
|
|
|
|
|
"type": 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) {
|
|
|
|
|
//服务器发送消息回调
|
|
|
|
|
let data = event.data;
|
|
|
|
|
try {
|
|
|
|
|
let msg = JSON.parse(data)
|
|
|
|
|
notice.info({
|
|
|
|
|
title: '[' + msg.businessTypeValue + ']' + msg.messageTitle,
|
|
|
|
|
message: msg.messageContent,
|
|
|
|
|
timeout: false
|
|
|
|
|
});
|
|
|
|
|
$('#messageDot').show();
|
|
|
|
|
let originalData = JSON.parse(data)
|
|
|
|
|
let func = msgTypeCallBackMap.get(originalData.type);
|
|
|
|
|
if (func) {
|
|
|
|
|
eval(func(originalData));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
@ -135,15 +165,19 @@
|
|
|
|
|
wsSend: function (event) {
|
|
|
|
|
//发送成功后的回调
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 注册系统消息监听
|
|
|
|
|
insertCallback(WEB_SOCKET_MSG_TYPE.SERVER.SYS_NOTICE_MSG_TYPE, function (massage) {
|
|
|
|
|
notice.info({
|
|
|
|
|
title: '[' + massage.data.businessTypeValue + ']' + massage.data.messageTitle,
|
|
|
|
|
message: massage.data.messageContent,
|
|
|
|
|
timeout: false
|
|
|
|
|
});
|
|
|
|
|
$('#messageDot').show();
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.unload = function () {
|
|
|
|
|
if (wsInst) {
|
|
|
|
|
wsInst.close();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
|
|