tcp server 用于给andriod 客户端定时发送消息
liuxiaolong
2019-11-04 d67203f7658672deb0895ab791ca1b583b668db0
realTime get data
2个文件已修改
28 ■■■■ 已修改文件
esutil/EsClient.go 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
server.go 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
esutil/EsClient.go
@@ -360,15 +360,13 @@
    return value[i-1]
}
func PostAction(sec int, Eurl string, picurl string, ishub string, size int) []byte {
func PostAction(sec int, Eurl string, picurl string, ishub string, size int, lastT time.Time, curTime time.Time) []byte {
    lastTimeStr := lastT.Format("2006-01-02 15:04:05")
    curTimeStr := curTime.Format("2006-01-02 15:04:05")
    index := "videopersons,personaction"
    url := fmt.Sprintf("%s%s%s", Eurl, index, "/_search")
    startTime := time.Now()
    seccond := strconv.Itoa(sec)
    preSec := ""
    if sec > 60 {
        preSec = "-60s"
    }
    sizeStr :=""
    if size <=0 {
        sizeStr = "100"
@@ -380,9 +378,9 @@
    if ishub == "hub" {
        filterArr = append(filterArr,"{\"term\":{\"personIsHub\":\"1\"}}")
    }
    if sec > 60 {
        filterArr = append(filterArr, "{\"range\":{\"picDate\":{\"gte\":\"now+8h-" + seccond + "s\",\"lt\":\"now+8h"+preSec+"\"}}}")
    }
    filterArr = append(filterArr, "{\"range\":{\"picDate\":{\"gte\":\""+lastTimeStr+"\",\"lt\":\""+curTimeStr+"\"}}}")
    filterStr := ""
    if len(filterArr) >0 {
        filterStr = strings.Join(filterArr, ",")
server.go
@@ -67,15 +67,17 @@
    }(conn, ech)
    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()
@@ -86,8 +88,8 @@
    log.Log.Infoln("Client at " + remoteAddr + " disconnected.")
}
func handleMessage(conn net.Conn, connArr []net.Conn) bool {
    jsonstring := esutil.PostAction(*sec, *Eurl, *Picurl, *IsHub, *Size)
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())
        if _, err := conn.Write([]byte("\000"));err !=nil {