Skip to content
Open
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
17 changes: 13 additions & 4 deletions packages/transport-websockets/src/websocket-to-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ class WebSocketMultiaddrConnection extends AbstractMultiaddrConnection {
this.websocket.send(buf)
}

const canSendMore = this.websocket.bufferedAmount < this.maxBufferedAmount
// Some WebSocket implementations (notably React Native) don't expose
// `bufferedAmount`. When unavailable, treat it as 0; we can't track
// backpressure, but at least writes proceed instead of blocking forever
// waiting for a 'drain' event, the implementation never emits.
const bufferedAmount = typeof this.websocket.bufferedAmount === 'number'
? this.websocket.bufferedAmount
: 0
const canSendMore = bufferedAmount < this.maxBufferedAmount

if (!canSendMore) {
this.checkBufferedAmountTask.start()
Expand Down Expand Up @@ -92,9 +99,11 @@ class WebSocketMultiaddrConnection extends AbstractMultiaddrConnection {
}

private checkBufferedAmount (): void {
this.log('buffered amount now %d', this.websocket.bufferedAmount)

if (this.websocket.bufferedAmount === 0) {
const bufferedAmount = typeof this.websocket.bufferedAmount === 'number'
? this.websocket.bufferedAmount
: 0
this.log('buffered amount now %d', bufferedAmount)
if (bufferedAmount === 0) {
this.checkBufferedAmountTask.stop()
this.safeDispatchEvent('drain')
}
Expand Down
Loading