Skip to content

Commit

Permalink
fix: only send data if the socket is open (#75)
Browse files Browse the repository at this point in the history
Sometimes the read promise resolves without error, but the socket
is in the `CLOSING` state.

This causes an error to be thrown so only send data if the socket
is `OPEN` - if it's `CLOSING` treat it as `CLOSED` and exit the
loop.
  • Loading branch information
achingbrain authored Aug 10, 2023
1 parent c05e56b commit f63265e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export default (socket: WebSocket, options: SinkOptions): Sink<Source<Uint8Array
throw err
}

// the ready promise resolved without error but the socket was closing so
// exit the loop and don't send data
if (socket.readyState === socket.CLOSING || socket.readyState === socket.CLOSED) {
break
}

socket.send(data)
}

Expand Down

2 comments on commit f63265e

@SgtPooki
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@SgtPooki
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achingbrain there is also a callback method on socket.send that we could use that has an error object

Please sign in to comment.