From 7f15cb77c303a910777906109e67f01390cd8ddb Mon Sep 17 00:00:00 2001 From: Bruce Marriner Date: Mon, 11 Jan 2016 18:37:59 -0600 Subject: [PATCH] Initial add of working udpKeepAlive function --- voice.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/voice.go b/voice.go index 7a43c46..08b1168 100644 --- a/voice.go +++ b/voice.go @@ -168,7 +168,15 @@ func (v *Voice) wsEvent(messageType int, message []byte) { // TODO monitor a chan/bool to verify this was successful // Start the UDP connection - v.udpOpen() + err := v.udpOpen() + if err != nil { + fmt.Println("Error opening udp connection: ", err) + return + } + + // start udpKeepAlive + go v.udpKeepAlive(5 * time.Second) + // TODO: find a way to check that it fired off okay return @@ -332,3 +340,28 @@ func (v *Voice) udpOpen() (err error) { v.Ready = true return } + +// udpKeepAlive sends a udp packet to keep the udp connection open +// This is still a bit of a "proof of concept" +func (v *Voice) udpKeepAlive(i time.Duration) { + + var err error + var sequence uint64 = 0 + + packet := make([]byte, 8) + + ticker := time.NewTicker(i) + for { + // TODO: Add a way to break from loop + + binary.LittleEndian.PutUint64(packet, sequence) + sequence++ + + _, err = v.UDPConn.Write(packet) + if err != nil { + fmt.Println("udpKeepAlive udp write error : ", err) + return + } + <-ticker.C + } +}