-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(websocket): fix timer blocking writes (#670)
An immediate setTimeout was used to unblock the WebSocket write. Unfortunately, this setTimeout can be throttled by browsers when the tab is backgrounded. This can lead to missed pong responses to server pings, which eventually leads to disconnection. Related: #649
- Loading branch information
1 parent
52847fd
commit f30a10b
Showing
3 changed files
with
17 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
const globalThis = require("../globalThis"); | ||
const nextTick = (() => { | ||
const isPromiseAvailable = | ||
typeof Promise === "function" && typeof Promise.resolve === "function"; | ||
if (isPromiseAvailable) { | ||
return cb => Promise.resolve().then(cb); | ||
} else { | ||
return cb => setTimeout(cb, 0); | ||
} | ||
})(); | ||
|
||
module.exports = { | ||
WebSocket: globalThis.WebSocket || globalThis.MozWebSocket, | ||
usingBrowserWebSocket: true, | ||
defaultBinaryType: "arraybuffer" | ||
defaultBinaryType: "arraybuffer", | ||
nextTick | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
WebSocket: require("ws"), | ||
usingBrowserWebSocket: false, | ||
defaultBinaryType: "nodebuffer" | ||
defaultBinaryType: "nodebuffer", | ||
nextTick: process.nextTick | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters