单点连接与切换窗口重连

pull/23/head
taoshihan1991 3 years ago
parent 796ae971e7
commit 0ea5292d57

@ -16,12 +16,10 @@ new Vue({
timer:null, timer:null,
sendDisabled:false, sendDisabled:false,
flyLang:GOFLY_LANG[LANG], flyLang:GOFLY_LANG[LANG],
connecting:true,
}, },
methods: { methods: {
//初始化websocket //初始化websocket
initConn:function() { initConn:function() {
this.connecting=true;
let socket = new ReconnectingWebSocket(this.server+"?visitor_id="+this.visitor.visitor_id);//创建Socket实例 let socket = new ReconnectingWebSocket(this.server+"?visitor_id="+this.visitor.visitor_id);//创建Socket实例
socket.maxReconnectAttempts = 30; socket.maxReconnectAttempts = 30;
this.socket = socket this.socket = socket
@ -33,10 +31,10 @@ new Vue({
}, },
OnOpen:function() { OnOpen:function() {
this.chatTitle=GOFLY_LANG[LANG]['connectok']; this.chatTitle=GOFLY_LANG[LANG]['connectok'];
this.connecting=false; this.socketClosed=false;
}, },
OnMessage:function(e) { OnMessage:function(e) {
this.socketClosed=false;
const redata = JSON.parse(e.data); const redata = JSON.parse(e.data);
if (redata.type == "kfOnline") { if (redata.type == "kfOnline") {
let msg = redata.data let msg = redata.data
@ -155,9 +153,9 @@ new Vue({
}, },
OnClose:function() { OnClose:function() {
this.socketClosed=true; this.socketClosed=true;
this.chatTitle="连接关闭!请重新打开页面"; // this.chatTitle="连接关闭!请重新打开页面";
$(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>"); // $(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
this.scrollBottom(); // this.scrollBottom();
}, },
//获取当前用户信息 //获取当前用户信息
getUserInfo:function(){ getUserInfo:function(){
@ -344,7 +342,7 @@ new Vue({
if(_this.socket!=null){ if(_this.socket!=null){
_this.socket.send(JSON.stringify(mes)); _this.socket.send(JSON.stringify(mes));
} }
},120000); },60000);
}, },
//初始化 //初始化
init:function(){ init:function(){
@ -361,10 +359,13 @@ new Vue({
$('.faceBox').hide(); $('.faceBox').hide();
}); });
window.onfocus = function () { window.onfocus = function () {
if(_this.connecting=true){ if(!_this.socketClosed){
return; return;
} }
_this.initConn(); _this.initConn();
_this.chatTitle="连接已重连";
$(".chatBox").append("<div class=\"chatTime\">"+_this.chatTitle+"</div>");
_this.scrollBottom();
} }
}, },
//表情点击事件 //表情点击事件

@ -63,6 +63,18 @@ func NewVisitorServer(c *gin.Context) {
} }
func AddVisitorToList(user *User) { func AddVisitorToList(user *User) {
//用户id对应的连接 //用户id对应的连接
oldUser, ok := ClientList[user.Id]
if oldUser != nil || ok {
msg := TypeMessage{
Type: "close",
Data: user.Id,
}
str, _ := json.Marshal(msg)
if err := oldUser.Conn.WriteMessage(websocket.TextMessage, str); err != nil {
oldUser.Conn.Close()
delete(ClientList, user.Id)
}
}
ClientList[user.Id] = user ClientList[user.Id] = user
lastMessage := models.FindLastMessageByVisitorId(user.Id) lastMessage := models.FindLastMessageByVisitorId(user.Id)
userInfo := make(map[string]string) userInfo := make(map[string]string)

Loading…
Cancel
Save