增加tcp在线用户接口

pull/30/head
taoshihan1991 4 years ago
parent a9c6ee3725
commit 5507195c71

@ -4,7 +4,7 @@ import (
"log" "log"
"net" "net"
) )
var clientTcpList = make([]net.Conn,0) var clientTcpList = make(map[string]net.Conn)
func NewTcpServer(tcpBaseServer string){ func NewTcpServer(tcpBaseServer string){
listener, err := net.Listen("tcp", tcpBaseServer) listener, err := net.Listen("tcp", tcpBaseServer)
if err != nil { if err != nil {
@ -18,15 +18,19 @@ func NewTcpServer(tcpBaseServer string){
log.Println("Error accepting", err.Error()) log.Println("Error accepting", err.Error())
return // 终止程序 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){ func PushServerTcp(str []byte){
for index,conn:=range clientTcpList{ for ip,conn:=range clientTcpList{
_,err:=conn.Write(str) _,err:=conn.Write(str)
log.Println(index,err) log.Println(ip,err)
if err!=nil{ if err!=nil{
clientTcpList=append(clientTcpList[:index],clientTcpList[index+1:]...) delete(clientTcpList,ip)
//clientTcpList=append(clientTcpList[:index],clientTcpList[index+1:]...)
} }
} }
} }

@ -133,9 +133,16 @@ func GetVisitorOnlines(c *gin.Context) {
userInfo["avator"] = visitor.avator userInfo["avator"] = visitor.avator
users=append(users,userInfo) users=append(users,userInfo)
} }
tcps:=make([]string,0)
for ip,_ :=range clientTcpList{
tcps=append(tcps,ip)
}
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"code": 200, "code": 200,
"msg": "ok", "msg": "ok",
"result":users, "result":gin.H{
"ws":users,
"tcp":tcps,
},
}) })
} }
Loading…
Cancel
Save