Skip to content

Ping the signaling server from the client #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2025
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
8 changes: 6 additions & 2 deletions internal/signaling/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,16 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
}
go metrics.RecordEvent(ctx, params)

case "pong":
case "ping", "pong":
// ignore, ping/pong is just for the tcp keepalive.

default:
if err := peer.HandlePacket(ctx, base.Type, raw); err != nil {
util.ErrorAndDisconnect(ctx, conn, err)
if err == ErrUnknownPacketType {
logger.Warn("unknown packet type received", zap.String("type", base.Type), zap.String("peer", peer.ID), zap.String("game", peer.Game), zap.String("origin", r.Header.Get("Origin")))
} else {
util.ErrorAndDisconnect(ctx, conn, err)
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/signaling/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (

const DefaultMaxPlayers = 4

var ErrUnknownPacketType = fmt.Errorf("unknown packet type")

type Peer struct {
store stores.Store
conn *websocket.Conn
Expand Down Expand Up @@ -167,7 +169,7 @@ func (p *Peer) HandlePacket(ctx context.Context, typ string, raw []byte) error {
}

default:
logger.Warn("unknown packet type received", zap.String("type", typ))
return ErrUnknownPacketType
}

return nil
Expand Down
19 changes: 19 additions & 0 deletions lib/signaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export default class Signaling extends EventEmitter<SignalingListeners> {
this.replayQueue = new Map()

this.ws = this.connect()

// Send a ping every 5 seconds to keep the connection alive,
// and to detect when the connection is lost.
setInterval(() => {
this.ping()
}, 5000)
}

private connect (): WebSocket {
Expand Down Expand Up @@ -141,6 +147,19 @@ export default class Signaling extends EventEmitter<SignalingListeners> {
}
}

ping (): void {
// Send a ping to the server to keep the connection alive,
// and to detect when the connection is lost.
// Sending is enough, we don't need to listen for a pong.
// Don't use this.send() as we don't need every ping to be logged.
if (this.ws.readyState === WebSocket.OPEN) {
const data = JSON.stringify({
type: 'ping'
})
this.ws.send(data)
}
}

private async handleSignalingMessage (data: string): Promise<void> {
try {
const packet = JSON.parse(data) as SignalingPacketTypes
Expand Down