Skip to content
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

support set ping handler and pong handler #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions conn_notjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Conn struct {
pingCounter int32
activePingsMu sync.Mutex
activePings map[string]chan<- struct{}
pingHandler func(ctx context.Context, p []byte) error
pongHandler func(ctx context.Context, p []byte) error
}

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

c.readMu = newMu(c)
c.writeFrameMu = newMu(c)
Expand Down
45 changes: 45 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,51 @@ 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.SetPingHandler(func(context.Context, []byte) error {
count++
return nil
})

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.cleanup()

var count int
c1.SetPongHandler(func(context.Context, []byte) error {
count++
return nil
})

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)
defer tt.cleanup()
Expand Down
28 changes: 26 additions & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ func (c *Conn) SetReadLimit(n int64) {
c.msgReader.limitReader.limit.Store(n + 1)
}

// SetPingHandler set the handler for pong handler
// From 5.5.2 of RFC 6455
// "Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response"
func (c *Conn) SetPingHandler(f func(ctx context.Context, p []byte) error) {
c.pingHandler = func(ctx context.Context, p []byte) error {
if err := c.writeControl(ctx, opPong, p); err != nil {
return err
}
if f != nil {
return f(ctx, p)
}
return nil
}
}

// SetPongHandler set the handler for ping message
// By default, do nothing
func (c *Conn) SetPongHandler(f func(ctx context.Context, p []byte) error) {
if f == nil {
f = func(context.Context, []byte) error { return nil }
}
c.pongHandler = f
}

const defaultReadLimit = 32768

func newMsgReader(c *Conn) *msgReader {
Expand Down Expand Up @@ -265,7 +289,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) (err error) {

switch h.opcode {
case opPing:
return c.writeControl(ctx, opPong, b)
return c.pingHandler(ctx, b)
case opPong:
c.activePingsMu.Lock()
pong, ok := c.activePings[string(b)]
Expand All @@ -276,7 +300,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) (err error) {
default:
}
}
return nil
return c.pongHandler(ctx, b)
}

defer func() {
Expand Down