From 319916a777780df40675c7e22d2520f524612330 Mon Sep 17 00:00:00 2001 From: liuxiaolong <736321739@qq.com> Date: 星期一, 11 十一月 2019 16:44:10 +0800 Subject: [PATCH] fix picDate to minisec --- server.go | 62 +++++++++++++------------------ 1 files changed, 26 insertions(+), 36 deletions(-) diff --git a/server.go b/server.go index f33830b..a3298d9 100644 --- a/server.go +++ b/server.go @@ -7,24 +7,28 @@ "strconv" "time" - "github.com/long/test/esutil" - log "github.com/long/test/log" + "andriodServer/esutil" + "andriodServer/extend/config" + log "andriodServer/log" ) -var addr = flag.String("addr", "0.0.0.0", "The address to listen to;") -var Eurl = flag.String("eurl", "http://192.168.1.182:9200/", "The port to listen on; ") -var Picurl = flag.String("picurl", "http://58.118.225.79:41242/", "picture url ") +var addr = flag.String("addr", "0.0.0.0", "The address to listen to") +var Eurl = flag.String("eurl", "http://192.168.1.182:9200/", "The port to listen on") +var Picurl = flag.String("picurl", "http://58.118.225.79:41242/", "picture url") -var port = flag.Int("port", 6000, "The port to listen on; ") -var sec = flag.Int("sec", 10, "the second for query data. ") +var port = flag.Int("port", 6000, "The port to listen on") +var sec = flag.Int("sec", 10, "the second for query data") var Level = flag.String("level","ErrorLevel","log level") -//var ipMap map[string]net.Conn +var IsHub = flag.String("hub", "hub", "hub is personIsHub=1") +var Size = flag.Int("size", 100, "size default is 100") +var env = flag.String("env", "config", "env set") + func main() { flag.Parse() log.SetLogLevel(*Level) - + config.Init(*env) fmt.Println(*port) src := *addr + ":" + strconv.Itoa(*port) listener, err := net.Listen("tcp", src) @@ -39,17 +43,8 @@ connArr:=make([]net.Conn,0) - //ipMap = make(map[string]net.Conn,0) for { conn, err := listener.Accept()// - - //clientIpPort := conn.RemoteAddr().String() - //index := strings.Index(clientIpPort, ":") - //ipStr := clientIpPort[:index-1] - //if oldConn,ok := ipMap[ipStr];ok { - // oldConn.Close() - //} - //ipMap[ipStr] = conn connArr = append(connArr,conn) if err != nil { @@ -71,43 +66,38 @@ ech <- err }(conn, ech) - go func(conArray []net.Conn) { - for { - if len(conArray)>50 { - for _,connEach :=range conArray{ - connEach.Close() - } - return - } - time.Sleep(200*time.Second) - } - }(connArr) - tick := time.NewTicker(10 * time.Second) + tick := time.NewTicker(3 * time.Second) + lastTime := time.Now() for { select { case <-tick.C: - if !handleMessage(conn, connArr){ + curTime := time.Now() + if !handleMessage(conn, connArr, lastTime, curTime){ conn.Close() return } + lastTime = curTime case err := <-ech: log.Log.Infoln(err, "remoteAddr ", remoteAddr, " close") conn.Close() return } - } log.Log.Infoln("Client at " + remoteAddr + " disconnected.") } -func handleMessage(conn net.Conn, connArr []net.Conn) bool { - jsonstring := esutil.PostAction(*sec, *Eurl, *Picurl) +func handleMessage(conn net.Conn, connArr []net.Conn,lastT time.Time, curTime time.Time) bool { + jsonstring := esutil.PostAction(*sec, *Eurl, *Picurl, *IsHub, *Size, lastT, curTime) if jsonstring == nil { log.Log.Infoln("the data is nil,remoteArr:",conn.RemoteAddr()) - conn.Write([]byte("\000")) - return true + if _, err := conn.Write([]byte("\000"));err !=nil { + log.Log.Infoln("conn.WriteErr:",err) + return false + } else { + return true + } } jsonstring = append(jsonstring, []byte("\000")...) log.Log.Infoln("jsonstring len: ", len(jsonstring), "\000 data: ", len("\000")) -- Gitblit v1.8.0