#signal parent function to return from goroutine

4 messages · Page 1 of 1 (latest)

potent grail
#
    go func() {
        for {
            err := conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(time.Second*10))
            if err != nil {
                fmt.Println(err)
                return
            }
            time.Sleep(time.Second * 2)
        }
    }()

    //read messages from the client
    for {
        _, data, err := conn.ReadMessage()
        if err != nil {
            fmt.Println(err)
            return
        }

        buffer <- data
    }

I have the above code, the go routine checks to see if the connection is alive every 10 seconds if its not how do I signal to the parents function to return? conn.Readmessage blocks execution.

viscid pollen
#

two ways:

  1. call SetReadDeadline, check a atomic variable every 10 seconds
  2. close the connection in the check loop, and ReadMessage will return
potent grail
potent grail
#

got it, i closed the channel in addition to the connection in the check loop