Skip to content
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
12 changes: 5 additions & 7 deletions ap/ap.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ func (ap *Accesspoint) init(ctx context.Context) (err error) {
return fmt.Errorf("failed initializing diffiehellman: %w", err)
}

// close previous connection if any
if ap.conn != nil {
_ = ap.conn.Close()
ap.conn = nil
}

// open connection to accesspoint
attempts := 0
for {
Expand All @@ -95,9 +89,13 @@ func (ap *Accesspoint) init(ctx context.Context) (err error) {
conn, err := proxy.Dial(ctx_, "tcp", addr)
cancel()
if err == nil {
// close previous connection if any
if ap.conn != nil {
_ = ap.conn.Close()
}

// we assign to ap.conn after because if Dial fails we'll have a nil ap.conn which we don't want
ap.conn = conn
// Successfully connected.
ap.log.Debugf("connected to %s", addr)
return nil
} else if attempts >= 6 {
Expand Down
10 changes: 7 additions & 3 deletions dealer/dealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,27 @@ func (d *Dealer) connect(ctx context.Context) error {
return fmt.Errorf("failed obtaining dealer access token: %w", err)
}

if conn, _, err := websocket.Dial(ctx, fmt.Sprintf("wss://%s/?access_token=%s", d.addr(ctx), accessToken), &websocket.DialOptions{
addr := d.addr(ctx)
if conn, _, err := websocket.Dial(ctx, fmt.Sprintf("wss://%s/?access_token=%s", addr, accessToken), &websocket.DialOptions{
HTTPClient: d.client,
HTTPHeader: http.Header{
"User-Agent": []string{librespot.UserAgent()},
},
}); err != nil {
return err
} else {
if d.conn != nil {
_ = d.conn.Close(websocket.StatusServiceRestart, "")
}

// we assign to d.conn after because if Dial fails we'll have a nil d.conn which we don't want
d.conn = conn
d.log.Debug(fmt.Sprintf("connected to %s", addr))
}

// remove the read limit
d.conn.SetReadLimit(math.MaxUint32)

d.log.Debugf("dealer connection opened")

return nil
}

Expand Down
Loading