Authored by 李奇

socket断开提示

... ... @@ -183,32 +183,6 @@ function pageInit() {
}
/**
* 连接socket
*/
function connectSocket() {
socketChat.init(Object.assign(originConf, {
onMessage: function(e) {
var jsonString = e.data;
var received = JSON.parse(jsonString);
socketConfCM.conversationId = received.newConversationId !== 0 ?
received.newConversationId :
received.conversationId;
getMessage(received);
},
connectFailCb: function () {
$('.connect-fail').fadeIn();
},
socketClosedCb: function () {
alert('链接已经断开了,请刷新后重试!');
}
}));
}
/**
* 处理发送消息
* @param e
* @param msgType
... ... @@ -300,6 +274,9 @@ function pageInit() {
* 客服已离线
*/
function csOffline(message) {
// 关闭连接
socketChat.close();
// 系统通知
systemTip('', message.content);
}
... ... @@ -367,6 +344,7 @@ function pageInit() {
</span>
</p>
</div>`;
// 隐藏人工
$iconMs.hide();
break;
... ... @@ -686,6 +664,36 @@ function pageInit() {
}
/**
* 连接socket
*/
function connectSocket() {
socketChat.init(Object.assign(originConf, {
onMessage: function(e) {
var jsonString = e.data;
var received = JSON.parse(jsonString);
socketConfCM.conversationId = received.newConversationId !== 0 ?
received.newConversationId :
received.conversationId;
getMessage(received);
},
onClose: function() {
$('.connect-fail').fadeIn();
},
connectFailCb: function() {
$('.connect-fail').fadeIn();
},
socketClosedCb: function() {
$('.connect-fail').fadeIn();
}
}));
}
/**
* 消息解析
* @param msgList
*/
... ... @@ -1124,6 +1132,7 @@ function pageInit() {
// 重新连线
$document.on('click', '.reconnect', function() {
$('.connect-fail').hide();
// 共通处理
beforeSendMsg();
connectSocket();
... ... @@ -1170,12 +1179,13 @@ function pageInit() {
}
// 拉取域名信息
(function () {
(function() {
$.ajax({
type: 'GET',
url: '/service/domains',
success: function(domains) {
configDomains = domains;
// url前缀添加
for (key in urls) {
if (urls.hasOwnProperty(key)) {
... ...
... ... @@ -29,49 +29,51 @@ function socketInit(opts) {
conversationMessage = options.conversationMessage;
param = JSON.stringify(conversationMessage);
if (window.WebSocket) {
setTimeout(function () {
times = 1;
socket = socketConnect();
connectId = setInterval(function () {
if(socket.readyState !== WebSocket.OPEN) {
if(times < 3) {
socket.close();
socket = socketConnect();
times++;
} else {
clearInterval(connectId);
// 连接失败回调
options.connectFailCb();
}
} else {
clearInterval(connectId);
}
}, 5000);
}, 1000);
}
/**
* 连接
* @returns {WebSocket}
*/
function socketConnect() {
var socket = new WebSocket(server + '/im?param=' + param);
var socketIns = new WebSocket(server + '/im?param=' + param);
socket.onmessage = options.onMessage || function() {
socketIns.onmessage = options.onMessage || function() {
console.log('received msg');
};
socket.onopen = options.onOpen || function() {
socketIns.onopen = options.onOpen || function() {
console.log('websocket is open');
};
socket.onclose = options.onClose || function() {
socketIns.onclose = options.onClose || function() {
console.log('websocket is closed');
};
return socket;
return socketIns;
}
if (window.WebSocket) {
setTimeout(function() {
times = 1;
socket = socketConnect();
connectId = setInterval(function() {
if (socket.readyState !== WebSocket.OPEN) {
if (times < 3) {
socket.close();
socket = socketConnect();
times++;
} else {
clearInterval(connectId);
// 连接失败回调
options.connectFailCb();
}
} else {
clearInterval(connectId);
}
}, 5000);
}, 1000);
}
}
... ... @@ -90,8 +92,18 @@ function sendMsg(msg) {
}
}
/**
* 关闭socket
*/
function closeSocket() {
if (socket) {
socket.close();
}
}
module.exports = {
init: socketInit,
close: closeSocket,
send: sendMsg
};
... ...