Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion gateway/gateway_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,14 @@ func (g *gatewayImpl) heartbeat() {
func (g *gatewayImpl) sendHeartbeat() {
g.config.Logger.Debug("sending heartbeat")

sequence := 0
if g.config.LastSequenceReceived != nil {
sequence = *g.config.LastSequenceReceived
}

ctx, cancel := context.WithTimeout(context.Background(), g.heartbeatInterval)
defer cancel()
if err := g.Send(ctx, OpcodeHeartbeat, MessageDataHeartbeat(*g.config.LastSequenceReceived)); err != nil {
if err := g.Send(ctx, OpcodeHeartbeat, MessageDataHeartbeat(sequence)); err != nil {
if errors.Is(err, discord.ErrShardNotConnected) || errors.Is(err, syscall.EPIPE) {
return
}
Expand Down
8 changes: 8 additions & 0 deletions gateway/gateway_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

import (
"fmt"
"strconv"

"github.com/disgoorg/json"
"github.com/disgoorg/snowflake/v2"
Expand Down Expand Up @@ -496,6 +497,13 @@ func (MessageDataUnknown) messageData() {}
// MessageDataHeartbeat is used to ensure the websocket connection remains open, and disconnect if not.
type MessageDataHeartbeat int

func (m MessageDataHeartbeat) MarshalJSON() ([]byte, error) {
if m == 0 {
return json.NullBytes, nil
}
return []byte(strconv.Itoa(int(m))), nil
}

func (MessageDataHeartbeat) messageData() {}

// MessageDataIdentify is the data used in IdentifyCommandData
Expand Down