forked from pothtonswer/discordmuffin
Data websocket Heartbeat now uses a chan instead of bool to track state
This commit is contained in:
parent
ed1076361e
commit
666cbeb6a6
2 changed files with 16 additions and 14 deletions
|
@ -92,8 +92,9 @@ type Session struct {
|
||||||
|
|
||||||
// Mutex/Bools for locks that prevent accidents.
|
// Mutex/Bools for locks that prevent accidents.
|
||||||
// TODO: Add channels.
|
// TODO: Add channels.
|
||||||
heartbeatLock sync.Mutex
|
|
||||||
heartbeatRunning bool
|
heartbeatLock sync.Mutex
|
||||||
|
heartbeatChan chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Message stores all data related to a specific Discord message.
|
// A Message stores all data related to a specific Discord message.
|
||||||
|
|
25
wsapi.go
25
wsapi.go
|
@ -407,10 +407,6 @@ func (s *Session) event(messageType int, message []byte) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// This heartbeat is sent to keep the Websocket conenction
|
|
||||||
// to Discord alive. If not sent, Discord will close the
|
|
||||||
// connection.
|
|
||||||
|
|
||||||
// Heartbeat sends regular heartbeats to Discord so it knows the client
|
// Heartbeat sends regular heartbeats to Discord so it knows the client
|
||||||
// is still connected. If you do not send these heartbeats Discord will
|
// is still connected. If you do not send these heartbeats Discord will
|
||||||
// disconnect the websocket connection after a few seconds.
|
// disconnect the websocket connection after a few seconds.
|
||||||
|
@ -418,28 +414,28 @@ func (s *Session) Heartbeat(i time.Duration) {
|
||||||
|
|
||||||
if s.wsConn == nil {
|
if s.wsConn == nil {
|
||||||
fmt.Println("No websocket connection exists.")
|
fmt.Println("No websocket connection exists.")
|
||||||
return // TODO need to return an error.
|
return // TODO need to return/log an error.
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make pretty and include chan
|
// Make sure Heartbeat is not already running
|
||||||
s.heartbeatLock.Lock()
|
s.heartbeatLock.Lock()
|
||||||
if s.heartbeatRunning {
|
if s.heartbeatChan != nil {
|
||||||
s.heartbeatLock.Unlock()
|
s.heartbeatLock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
s.heartbeatChan = make(chan struct{})
|
||||||
s.heartbeatLock.Unlock()
|
s.heartbeatLock.Unlock()
|
||||||
|
|
||||||
defer func() { s.heartbeatRunning = false }()
|
defer close(s.heartbeatChan)
|
||||||
s.heartbeatRunning = true
|
|
||||||
|
|
||||||
// send first heartbeat immediately because lag could put the
|
// send first heartbeat immediately because lag could put the
|
||||||
// first heartbeat outside the required heartbeat interval window
|
// first heartbeat outside the required heartbeat interval window
|
||||||
ticker := time.NewTicker(i * time.Millisecond)
|
ticker := time.NewTicker(i * time.Millisecond)
|
||||||
for {
|
for {
|
||||||
timestamp := int(time.Now().Unix())
|
|
||||||
err := s.wsConn.WriteJSON(map[string]int{
|
err := s.wsConn.WriteJSON(map[string]int{
|
||||||
"op": 1,
|
"op": 1,
|
||||||
"d": timestamp,
|
"d": int(time.Now().Unix()),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error sending data heartbeat:", err)
|
fmt.Println("error sending data heartbeat:", err)
|
||||||
|
@ -447,7 +443,12 @@ func (s *Session) Heartbeat(i time.Duration) {
|
||||||
return // TODO log error?
|
return // TODO log error?
|
||||||
}
|
}
|
||||||
s.DataReady = true
|
s.DataReady = true
|
||||||
<-ticker.C
|
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
case <-s.heartbeatChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue