63 lines
2.2 KiB
JavaScript
63 lines
2.2 KiB
JavaScript
/**
|
|
* 进行检测socket登录
|
|
*/
|
|
function checkSocketLogin(uid, token, url, go_url) {
|
|
layui.use(['layer'], function () {
|
|
var layer = layui.layer;
|
|
});
|
|
// 返回结果如果为true的话那么就创建新链接反之不创建
|
|
var ws = new WebSocket(url);
|
|
// 打开
|
|
ws.onopen = function (evt) {
|
|
// console.log(evt);
|
|
console.log('连接成功');
|
|
// 登录
|
|
var data = '{"path":"api\/checkRepetitionLine","param":{"type":"kf", "uid" : ' + uid + '},"access_token" : "' + token + '"}';
|
|
ws.send(data);
|
|
console.log('发送消息');
|
|
};
|
|
|
|
ws.onmessage = function (res) {
|
|
layui.use(['layer'], function () {
|
|
var layer = layui.layer;
|
|
});
|
|
var data = eval("(" + res.data + ")");
|
|
console.log(data);
|
|
switch (data['message_type']) {
|
|
// 服务端ping客户端
|
|
case 'login_check':
|
|
if (data.data.result != true) {
|
|
// 登录时进出
|
|
if (go_url) {
|
|
setTimeout(function () {
|
|
// window.history.go(0);
|
|
window.location.href = go_url;
|
|
}, 500);
|
|
return;
|
|
}
|
|
// window.history.go(0);
|
|
sessionStorage.setItem('kf_check_line_' + uid, true);
|
|
return;
|
|
}
|
|
// 程序中使用
|
|
if (go_url == '') {
|
|
return layer.msg('聊天室链接已存在');
|
|
}
|
|
|
|
var l_win = layer.confirm('当前账号已登录是否继续登录?', { title: false, closeBtn: 0 }, function () {
|
|
layer.close(l_win);
|
|
var data = '{"path":"api\/closeRepetitionLine","param":{"type":"kf", "uid" : ' + uid + '},"access_token" : "' + token + '"}';
|
|
ws.send(data);
|
|
setTimeout(function () {
|
|
// window.history.go(0);
|
|
window.location.href = go_url;
|
|
}, 500);
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
|
|
ws.onclose = function () {
|
|
console.log('连接已关闭');
|
|
};
|
|
} |