Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 196a255

Browse files
authored
fix: Return error on pinging closed connections (#373)
1 parent 14062a1 commit 196a255

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

wsnet/dial.go

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ func (d *Dialer) Close() error {
176176

177177
// Ping sends a ping through the control channel.
178178
func (d *Dialer) Ping(ctx context.Context) error {
179+
if d.ctrl.ReadyState() == webrtc.DataChannelStateClosed || d.ctrl.ReadyState() == webrtc.DataChannelStateClosing {
180+
return webrtc.ErrConnectionClosed
181+
}
179182
// Since we control the client and server we could open this
180183
// data channel with `Negotiated` true to reduce traffic being
181184
// sent when the RTC connection is opened.

wsnet/dial_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ func TestDial(t *testing.T) {
161161
return
162162
}
163163
})
164+
165+
t.Run("Disconnect", func(t *testing.T) {
166+
connectAddr, listenAddr := createDumbBroker(t)
167+
_, err := Listen(context.Background(), listenAddr)
168+
if err != nil {
169+
t.Error(err)
170+
return
171+
}
172+
dialer, err := DialWebsocket(context.Background(), connectAddr, nil)
173+
if err != nil {
174+
t.Error(err)
175+
return
176+
}
177+
err = dialer.Close()
178+
if err != nil {
179+
t.Error(err)
180+
return
181+
}
182+
err = dialer.Ping(context.Background())
183+
if err != webrtc.ErrConnectionClosed {
184+
t.Error(err)
185+
}
186+
})
164187
}
165188

166189
func BenchmarkThroughput(b *testing.B) {

0 commit comments

Comments
 (0)