diff --git a/lib/cdp/websocket.go b/lib/cdp/websocket.go index 2179cb90..911bcd92 100644 --- a/lib/cdp/websocket.go +++ b/lib/cdp/websocket.go @@ -35,6 +35,10 @@ type WebSocket struct { // Connect to browser func (ws *WebSocket) Connect(ctx context.Context, wsURL string, header http.Header) error { + if ws.conn != nil { + panic("duplicated connection: " + wsURL) + } + ctx, cancel := context.WithCancel(ctx) ws.close = cancel diff --git a/lib/cdp/websocket_test.go b/lib/cdp/websocket_test.go index 7fb05882..8564b6bb 100644 --- a/lib/cdp/websocket_test.go +++ b/lib/cdp/websocket_test.go @@ -76,3 +76,17 @@ func (t T) newPage(ctx context.Context) (*cdp.Client, string) { return client, sessionID } + +func (t T) DuplicatedConnectErr() { + l := launcher.New() + t.Cleanup(l.Kill) + + u := l.MustLaunch() + + ws := &cdp.WebSocket{} + t.E(ws.Connect(t.Context(), u, nil)) + + t.Panic(func() { + _ = ws.Connect(t.Context(), u, nil) + }) +}