美文网首页Golang语言社区
Go语言多任务超时处理-channel实现demo

Go语言多任务超时处理-channel实现demo

作者: Golang语言社区 | 来源:发表于2019-01-17 16:27 被阅读15次

    demo 如下:

    package main
    
    import (
        "fmt"
        "net/http"
        "time"
    )
    
    var chandata chan map[int]int
    
    func init() {
        chandata = make(chan map[int]int)
        go TTimerAddData()
        go TTimeGetData()
    
    }
    
    // 压入数据测试
    func TTimerAddData() {
    
        vcount := 1
        keycount := 10000
    
        for {
            select {
            case <-time.After(time.Second * 10):
                {
                    data := make(map[int]int)
                    data[keycount] = vcount
                    vcount++
                    keycount++
                    chandata <- data
                }
            }
        }
    }
    
    // 获取数据测试
    func TTimeGetData() {
        for {
    
            select {
            case <-time.After(time.Second * 1):
                {
                }
            case i := <-chandata:
                {
                    for v := range i {
                        fmt.Println("-----------------i", i)
                        fmt.Println("-----------------v", v)
                        fmt.Println("-----------------vi", i[v])
                    }
                }
            }
        }
    }
    
    func main() {
        strport := "8892" //  GM 系统操作 -- 修改金币等操作
        http.HandleFunc("/GolangLtdGM", IndexHandlerGM)
        http.ListenAndServe(":"+strport, nil)
        return
    }
    
    
    输出结果

    LollipopGo 子游戏超时机制 部分代码 如下:

    func CheckGameOfPlayerLeftTime(iRoomID int, conn *websocket.Conn) {
        icount := 0
        iRoomIDbak := iRoomID
        for {
            select {
            case <-time.After(DSQTimeSpeed / 2):
                {
                    //--------------------------------------------------------------
                    ilen := len(TimeOutDSQ)
                    iilen := 0
                    tmp := make(map[int]int)
                    for v := range TimeOutDSQ {
                        fmt.Println("-==-=-=--=--key", v)
                        fmt.Println("-==-=-=--=--valve", TimeOutDSQ[v])
                        if v == iRoomIDbak {
                            icount = 0
                            continue
                        }
                        tmp[v] = TimeOutDSQ[v]
                        iilen++
                    }
                    TimeOutDSQ <- tmp
                    //--------------------------------------------------------------
                    if iilen == ilen {
                        icount++
                    }
                    if icount >= 30 {
                        res, err1 := cacheDSQ.Value(iRoomID)
                        if err1 != nil {
                            continue
                        }
                        data := &Proto2.DSQ2GW_BroadCast_GameOver{
                            Protocol:  Proto.G_GameDSQ_Proto,
                            Protocol2: Proto2.DSQ2GW_BroadCast_GameOverProto2,
                            IsDraw:    false,
                        }
                        openidtmp := GetPlayerChupaiGoround(iRoomIDbak)
                        data.OpenIDA = openidtmp
                        if res.Data().(*RoomPlayerDSQ).OpenIDA != openidtmp {
                            data.OpenIDB = res.Data().(*RoomPlayerDSQ).OpenIDA
                        } else {
                            data.OpenIDB = res.Data().(*RoomPlayerDSQ).OpenIDB
                        }
                        PlayerSendToServer(conn, data)
                        runtime.Goexit()
                        return
                    }
                }
            }
        }
    }
    

          每天坚持学习1小时Go语言,大家加油,我是彬哥,下期见!如果文章中不同观点、意见请文章下留言或者关注下方订阅号反馈!


    社区交流群:221273219
    Golang语言社区论坛 :
    www.Golang.Ltd
    LollipopGo游戏服务器地址:
    https://github.com/Golangltd/LollipopGo
    社区视频课程课件GIT地址:
    https://github.com/Golangltd/codeclass


    Golang语言社区

    相关文章

      网友评论

        本文标题:Go语言多任务超时处理-channel实现demo

        本文链接:https://www.haomeiwen.com/subject/xyiidqtx.html