123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package pubsub
- import (
- "fmt"
- "time"
- "github.com/gorilla/websocket"
- )
- func go_pinger(c *Connection) {
- // Pinger (sender)
- go func(c *Connection) {
- for {
- select {
- case <-time.After(1 * time.Second):
- if c.active && !c.ping_sended {
- if time.Since(c.ping_start) > TwitchApiPingEach {
- if err := c.Connection.WriteMessage(
- websocket.TextMessage,
- Answer{Type: Ping}.JSON(),
- ); err != nil {
- c.onError(err)
- c.active = false
- c.onDisconnect()
- } else {
- c.ping_start = time.Now()
- c.ping_sended = true
- c.onPing(c.ping_start)
- }
- }
- }
- case <-c.done:
- return
- }
- }
- }(c)
- // Pinger (handler)
- go func(c *Connection) {
- for {
- select {
- case <-time.After(1 * time.Second):
- if c.active && c.ping_sended {
- if time.Since(c.ping_start) > TwitchApiPingTimeout {
- c.onInfo(fmt.Sprintf("warning, no %s response more than %d seconds", Pong, TwitchApiPingTimeout))
- c.active = false
- c.onDisconnect()
- c.ping_start = time.Now()
- c.ping_sended = false
- if err := c.Connection.Close(); err != nil {
- c.onError(err)
- }
- }
- }
- case <-c.done:
- return
- }
- }
- }(c)
- }
|