(Go)比较ws循环中的变换变量

时间:2015-11-01 18:41:59

标签: go websocket gorilla

在一个接收消息并相应处理它们的循环上工作,基本上是一个带有保持活动和身份验证的websocket echo-er,而且我已经在keep-alive部分停留了一段时间。

概念很简单,当服务器启动时我创建一个带有自动收报机的goroutine,并且我初始化一个uint64指针,每次该自动收报机滴答(每2秒),我用atomic.AddUint64(clockTicks,1)递增指针),然后对于每个websocket连接goroutine,我用compare和atomic.LoadUint64(clockTicks)检查每个tick的变量,然后发送一个ping / pong消息。

编辑:似乎有些东西阻止了for循环,直到收到一条消息,结果:

i := atomic.LoadUint64(clockTicks)
    if i != cur {
        cur = i
        if act != true {
            fmt.Println("Quit Nao U filthy bot.")
            return
        } else {
            fmt.Println("Keep Going.")
            act = false
        }
    }

在这个片段中,i:= atomic.LoadUint64(clockTicks)&所有if块仅在我发送消息时运行(在msg上打印“Keep Going。”),这不是我想要的,我希望片段每次迭代运行,并且“继续前进”。 &安培;每次clockTicks递增时,“退出nao ...”触发

这是重要的代码部分,我正在使用Go和Gorilla的Websockets库:

func Clock() {
clockTicks = new(uint64)
*clockTicks = 0
clock := time.NewTicker(authIntervals).C
for {
    <-clock
    atomic.AddUint64(clockTicks, 1)
}
}

var wsu = websocket.Upgrader{
ReadBufferSize:  1024,
WriteBufferSize: 1024,
CheckOrigin:     OriginHandler,
}

func serveWS(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
    http.Error(w, "Method not allowed", 405)
    return
}

ws, err := wsu.Upgrade(w, r, nil)
if err != nil {
    fmt.Println(err)
    return
}

defer ws.Close()
cur := atomic.LoadUint64(clockTicks)
var act, val = true, false
for {
    i := atomic.LoadUint64(clockTicks)
    if i != cur { /* Only triggers when I receive a msg */
        cur = i
        if act != true {
            fmt.Println("Quit Nao U filthy bot.")
            return
        } else {
            fmt.Println("Keep Going.")
            act = false
        }
    }

    mtype, p, err := ws.ReadMessage()
    if err != nil {
        return
    }

    ...

}

编辑2:IRC中有人建议也许ws.ReadMessage是阻塞的,但我不太确定(他说在ws.ReadMessage实现中使用的ioutil.ReadAll阻止了它,而且他非常肯定)

1 个答案:

答案 0 :(得分:4)

websocket读取方法调用network connection Read method以从网络获取数据。由于网络连接Read方法块,webscocket方法也会阻塞。

要发送ping,请在写入循环as in the Gorilla chat example中使用自动收录器,或为ping as in Gorilla command example运行单独的goroutine。