Skip to content

Commit

Permalink
Add the ability to listen to when pings are recieved and pongs are sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw committed Apr 5, 2024
1 parent e95a7f5 commit 7ebde41
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/nhooyr.io/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type Conn struct {
pingCounter int32
activePingsMu sync.Mutex
activePings map[string]chan<- struct{}
pingListener func(context.Context, []byte)
pongListener func(context.Context, []byte)
}

type connConfig struct {
Expand Down Expand Up @@ -112,6 +114,9 @@ func newConn(cfg connConfig) *Conn {
closed: make(chan struct{}),
activePings: make(map[string]chan<- struct{}),
}
// set default ping, pong handler
c.SetPingListener(nil)
c.SetPongListener(nil)

c.readMu = newMu(c)
c.writeFrameMu = newMu(c)
Expand Down
43 changes: 43 additions & 0 deletions internal/nhooyr.io/websocket/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,49 @@ func TestConn(t *testing.T) {
assert.Contains(t, err, "failed to wait for pong")
})

t.Run("pingHandler", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, nil, nil)
//defer tt.Cleanup()

var count int
c2.SetPingListener(func(context.Context, []byte) {
count++
})

c1.CloseRead(tt.ctx)
c2.CloseRead(tt.ctx)

for i := 0; i < 10; i++ {
err := c1.Ping(tt.ctx)
assert.Success(t, err)
}

err := c1.Close(websocket.StatusNormalClosure, "")
assert.Success(t, err)
assert.Equal(t, "count", 10, count)
})

t.Run("pongHandler", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, nil, nil)
//defer tt.t.Cleanup()

var count int
c1.SetPongListener(func(context.Context, []byte) {
count++
})

c1.CloseRead(tt.ctx)
c2.CloseRead(tt.ctx)
for i := 0; i < 10; i++ {
err := c1.Ping(tt.ctx)
assert.Success(t, err)
}

err := c1.Close(websocket.StatusNormalClosure, "")
assert.Success(t, err)
assert.Equal(t, "count", 10, count)
})

t.Run("concurrentWrite", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, nil, nil)

Expand Down
18 changes: 18 additions & 0 deletions internal/nhooyr.io/websocket/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
return ctx
}

// SetPingListener calls the provided function when a ping is received.
func (c *Conn) SetPingListener(f func(context.Context, []byte)) {
if f == nil {
f = func(context.Context, []byte) {}
}
c.pingListener = f
}

// SetPongListener calls the provided function when a pong is sent.
func (c *Conn) SetPongListener(f func(context.Context, []byte)) {
if f == nil {
f = func(context.Context, []byte) {}
}
c.pongListener = f
}

// SetReadLimit sets the max number of bytes to read for a single message.
// It applies to the Reader and Read methods.
//
Expand Down Expand Up @@ -297,8 +313,10 @@ func (c *Conn) handleControl(ctx context.Context, h header) (err error) {

switch h.opcode {
case opPing:
c.pingListener(ctx, b)
return c.writeControl(ctx, opPong, b)
case opPong:
c.pongListener(ctx, b)
c.activePingsMu.Lock()
pong, ok := c.activePings[string(b)]
c.activePingsMu.Unlock()
Expand Down

0 comments on commit 7ebde41

Please sign in to comment.