From 5507195c71b8e26409e8975e4002c76e00ea987c Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Thu, 20 Aug 2020 17:35:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0tcp=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/tcp.go | 14 +++++++++----- controller/visitor.go | 9 ++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/controller/tcp.go b/controller/tcp.go index ae6614f..9337b37 100644 --- a/controller/tcp.go +++ b/controller/tcp.go @@ -4,7 +4,7 @@ import ( "log" "net" ) -var clientTcpList = make([]net.Conn,0) +var clientTcpList = make(map[string]net.Conn) func NewTcpServer(tcpBaseServer string){ listener, err := net.Listen("tcp", tcpBaseServer) if err != nil { @@ -18,15 +18,19 @@ func NewTcpServer(tcpBaseServer string){ log.Println("Error accepting", err.Error()) return // 终止程序 } - clientTcpList=append(clientTcpList,conn) + var remoteIpAddress = conn.RemoteAddr() + clientTcpList[remoteIpAddress.String()]=conn + log.Println(remoteIpAddress,clientTcpList) + //clientTcpList=append(clientTcpList,conn) } } func PushServerTcp(str []byte){ - for index,conn:=range clientTcpList{ + for ip,conn:=range clientTcpList{ _,err:=conn.Write(str) - log.Println(index,err) + log.Println(ip,err) if err!=nil{ - clientTcpList=append(clientTcpList[:index],clientTcpList[index+1:]...) + delete(clientTcpList,ip) + //clientTcpList=append(clientTcpList[:index],clientTcpList[index+1:]...) } } } \ No newline at end of file diff --git a/controller/visitor.go b/controller/visitor.go index a050d9b..00d226e 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -133,9 +133,16 @@ func GetVisitorOnlines(c *gin.Context) { userInfo["avator"] = visitor.avator users=append(users,userInfo) } + tcps:=make([]string,0) + for ip,_ :=range clientTcpList{ + tcps=append(tcps,ip) + } c.JSON(200, gin.H{ "code": 200, "msg": "ok", - "result":users, + "result":gin.H{ + "ws":users, + "tcp":tcps, + }, }) } \ No newline at end of file