From 0ea5292d578722b614bb15204023ff0f55c14b6b Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Wed, 24 Feb 2021 10:13:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E7=82=B9=E8=BF=9E=E6=8E=A5=E4=B8=8E?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E7=AA=97=E5=8F=A3=E9=87=8D=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/js/chat-page.js | 19 ++++++++++--------- ws/visitor.go | 12 ++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/static/js/chat-page.js b/static/js/chat-page.js index 28040e6..e8b0f51 100644 --- a/static/js/chat-page.js +++ b/static/js/chat-page.js @@ -16,12 +16,10 @@ new Vue({ timer:null, sendDisabled:false, flyLang:GOFLY_LANG[LANG], - connecting:true, }, methods: { //初始化websocket initConn:function() { - this.connecting=true; let socket = new ReconnectingWebSocket(this.server+"?visitor_id="+this.visitor.visitor_id);//创建Socket实例 socket.maxReconnectAttempts = 30; this.socket = socket @@ -33,10 +31,10 @@ new Vue({ }, OnOpen:function() { this.chatTitle=GOFLY_LANG[LANG]['connectok']; - this.connecting=false; - + this.socketClosed=false; }, OnMessage:function(e) { + this.socketClosed=false; const redata = JSON.parse(e.data); if (redata.type == "kfOnline") { let msg = redata.data @@ -155,9 +153,9 @@ new Vue({ }, OnClose:function() { this.socketClosed=true; - this.chatTitle="连接关闭!请重新打开页面"; - $(".chatBox").append("
"+this.chatTitle+"
"); - this.scrollBottom(); + // this.chatTitle="连接关闭!请重新打开页面"; + // $(".chatBox").append("
"+this.chatTitle+"
"); + // this.scrollBottom(); }, //获取当前用户信息 getUserInfo:function(){ @@ -344,7 +342,7 @@ new Vue({ if(_this.socket!=null){ _this.socket.send(JSON.stringify(mes)); } - },120000); + },60000); }, //初始化 init:function(){ @@ -361,10 +359,13 @@ new Vue({ $('.faceBox').hide(); }); window.onfocus = function () { - if(_this.connecting=true){ + if(!_this.socketClosed){ return; } _this.initConn(); + _this.chatTitle="连接已重连"; + $(".chatBox").append("
"+_this.chatTitle+"
"); + _this.scrollBottom(); } }, //表情点击事件 diff --git a/ws/visitor.go b/ws/visitor.go index 4a2a1aa..05f0c5b 100644 --- a/ws/visitor.go +++ b/ws/visitor.go @@ -63,6 +63,18 @@ func NewVisitorServer(c *gin.Context) { } func AddVisitorToList(user *User) { //用户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 lastMessage := models.FindLastMessageByVisitorId(user.Id) userInfo := make(map[string]string)