Skip to content
This repository was archived by the owner on Dec 3, 2021. It is now read-only.
Closed
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
21 changes: 20 additions & 1 deletion robust-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
}
})(function(global, navigator) {

var WebSocket = global.WebSocket
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't make sense. either a) WebSocket is native and supported and never replaced, or b) it is polyfilled and we shouldn't close over and force the users to understand ordering of this library and the polyfill.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The reason for this change is for another option: WebSocket has been overriden by browser extensions and users want to ensure that RobustWebSocket's implementation doesn't change from under them.


var RobustWebSocket = function(url, protocols, userOptions) {
var realWs = { close: function() {} },
connectTimeout,
Expand Down Expand Up @@ -75,6 +77,12 @@
}

self.send = function() {
if (realWs.readyState !== self.OPEN) {
throw Object.assign(
new Error('An attempt was made to use a WebSocket that is not usable'),
{ name: 'InvalidStateError' }
)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why would we try to re-implement the WebSocket standard? calling realWs.send.apply(...) should just throw this error.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

As previously stated - realWs.send is not always present

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But that's not what this code is checking. If you want to check for th3 existence of send and throw an Error, that is acceptible.

return realWs.send.apply(realWs, arguments)
}

Expand All @@ -93,7 +101,7 @@
}

self.open = function() {
if (realWs.readyState !== WebSocket.OPEN && realWs.readyState !== WebSocket.CONNECTING) {
if (realWs.readyState !== self.OPEN && realWs.readyState !== self.CONNECTING) {
clearPendingReconnectIfNeeded()
reconnectWhenOnlineAgain = false
explicitlyClosed = false
Expand Down Expand Up @@ -239,5 +247,16 @@
}
}

['CONNECTING' , 'OPEN', 'CLOSING', 'CLOSED'].forEach(function (name) {
const descriptor = {
writable: false,
enumerable: true,
configurable: false,
value: WebSocket[name],
}
Object.defineProperty(RobustWebSocket, name, descriptor)
Object.defineProperty(RobustWebSocket.prototype, name, descriptor)
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd be glad to take this change at robust-websocket's new home, nathanboktae/robust-websocket


return RobustWebSocket
}, typeof window != 'undefined' ? window : (typeof global != 'undefined' ? global : this));