-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Clean up and simplify WebSocket implementation on the JS side #6889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@philikon updated the pull request. |
@philikon updated the pull request. |
@philikon Nice! I'll should be able to pull this into the current project I'm working on to test. I've noticed you've been working on a couple stuff with Websockets lately and was just curious how your testing these new features and if you so on both iOS and Android? Thanks again! |
|
||
cancelConnectionImpl(): void { | ||
this.readyState = this.CLOSING; | ||
this._closeWebSocket(this._socketId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec allows to specify a reason, and the Android implementation supports it also. Can you add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Can I do that in a follow-up? It'd be a new feature. I wanted to keep this PR here purely mechanical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@philikon Not a new feature really, since closeConnectionImpl
already has it, and it was left out in the close
method. But I'm fine with either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the original close()
method:
close(): void {
if (this.readyState === this.CLOSING ||
this.readyState === this.CLOSED) {
return;
}
if (this.readyState === this.CONNECTING) {
this.cancelConnectionImpl();
}
this.readyState = this.CLOSING;
this.closeConnectionImpl();
}
So, yes, while closeConnectionImpl()
takes those parameters, close()
never passed them on. Happy to add it, though.
Can you provide a test plan? |
event = new WebSocketEvent('close'); | ||
event.message = ev.message; | ||
this.dispatchEvent(event); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: unnecessary new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can remove the newlines. I just added them to separate the repetitive stanzas for easier readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I just removed the newlines and I find the method so much harder to parse now. What's the reasoning here for declaring the newline "unnecessary"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@philikon I prefer a lot of new lines in code, but that's not how React Native's authors prefer it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we're in agreement then. :)
I think I can count myself as one of React Native's authors, so I'll just leave them in there until somebody complaints.
There's some inconsistency regarding where you pass the |
Thanks for pointing that out. I'll fix that.
I don't think the number of keypresses should be a measure for how we structure code. That kind of metric leads to overly short variable names that are no longer descriptive. Plus, I haven't seen your preferred style used anywhere else in React Native, in particular not in XMLHttpRequest, which is another similar web API that's we implemented for RN. Consistency within the project is what I'm mostly after, because it inspires 3rd party code. (Case in point: react-native-webrtc used the WebSocket implementation in RN as inspiration, which means it cargo-culted all of the unnecessary indirections etc. without questioning it.) |
That was not the measure I used, my point was that it makes easier to refactor things which I said above, since making it a pure function decreases the coupling of the method to the class. I think this pattern is better than tightly coupled class methods. This incidentally also helps in decreasing keypresses, which I noticed and mentioned. You could name it
I don't think there's any preferred style anywhere in the codebase. Looking at |
- Get rid of no longer necessary WebSocket.js v WebSocketBase.js split - Use `EventTarget(list, of, events)` as base class to auto-generate `oneventname` getters/setters that get invoked along with other event handlers - Type annotation `any` considered harmful, especially when we can easily spell out the actual type - Throw in some `const` goodness for free
Yep, and more to come. I'm planning on picking up #6327 and getting it into shape to land.
Ah, yes. I have a little test script + app. I should just add those as a UIExplorer example! |
@philikon updated the pull request. |
}; | ||
|
||
_sendText = () => { | ||
this.state.socket.send(this.state.outgoingMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call of method send
Method cannot be called on possibly null value null
@philikon updated the pull request. |
This PR and #6961 both add the example, which one should be merged? |
Thanks for doing this! The test plan looks good. Shall we #shipit ? |
@facebook-github-bot shipit |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
What's blocking this from getting merged at this point? |
Summary:This brings the same functionality that's already present on iOS, introduced in #4483, to Android: convert binary payloads to base64 strings and send them to JS land that way, where they'll be turned into an ArrayBuffer. **Test Plan:** Used test server from #6889 (in `--binary` mode) to send some binary data to the Android UIExplorer example (also from #6889). Verified it's received correctly as `ArrayBuffer`. Closes #6868 Differential Revision: D3184797 Pulled By: mkonicek fb-gh-sync-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d fbshipit-source-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d
@philikon Failed landcastle with unrelated errors which is quite common :( Just hit 'Re-ship' for you and will keep an eye on the status. |
Woo, thanks! |
Summary:This is a reprise of #6327, but with iOS 7.0 compatibility and less `package.json` changes. **Test Plan:** Load WebSocketExample in UIExplorer app and start websocket test server script (both provided in #6889) and test sending binary data on both iOS and Android Closes #6961 Differential Revision: D3202022 Pulled By: mkonicek fb-gh-sync-id: 38843d0a9c0172971c5c70a5139ded04042b280a fbshipit-source-id: 38843d0a9c0172971c5c70a5139ded04042b280a
Summary:This brings the same functionality that's already present on iOS, introduced in facebook#4483, to Android: convert binary payloads to base64 strings and send them to JS land that way, where they'll be turned into an ArrayBuffer. **Test Plan:** Used test server from facebook#6889 (in `--binary` mode) to send some binary data to the Android UIExplorer example (also from facebook#6889). Verified it's received correctly as `ArrayBuffer`. Closes facebook#6868 Differential Revision: D3184797 Pulled By: mkonicek fb-gh-sync-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d fbshipit-source-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d
Summary:- Get rid of no longer necessary WebSocket.js v WebSocketBase.js split - Use `EventTarget(list, of, events)` as base class to auto-generate `oneventname` getters/setters that get invoked along with other event handlers - Type annotation `any` considered harmful, especially when we can easily spell out the actual type - Throw in some `const` goodness for free **Test Plan:** Launch UIExplorer example app, supplied `websocket_test_server` script, and try different combinations of sending and receiving text and binary data on both iOS and Android. Closes facebook#6889 Differential Revision: D3184835 Pulled By: mkonicek fb-gh-sync-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd fbshipit-source-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd
Summary:This is a reprise of facebook#6327, but with iOS 7.0 compatibility and less `package.json` changes. **Test Plan:** Load WebSocketExample in UIExplorer app and start websocket test server script (both provided in facebook#6889) and test sending binary data on both iOS and Android Closes facebook#6961 Differential Revision: D3202022 Pulled By: mkonicek fb-gh-sync-id: 38843d0a9c0172971c5c70a5139ded04042b280a fbshipit-source-id: 38843d0a9c0172971c5c70a5139ded04042b280a
Summary:This is a reprise of #6327, but with iOS 7.0 compatibility and less `package.json` changes. **Test Plan:** Load WebSocketExample in UIExplorer app and start websocket test server script (both provided in #6889) and test sending binary data on both iOS and Android Closes facebook/react-native#6961 Differential Revision: D3202022 Pulled By: mkonicek fb-gh-sync-id: 38843d0a9c0172971c5c70a5139ded04042b280a fbshipit-source-id: 38843d0a9c0172971c5c70a5139ded04042b280a
Summary:This brings the same functionality that's already present on iOS, introduced in facebook#4483, to Android: convert binary payloads to base64 strings and send them to JS land that way, where they'll be turned into an ArrayBuffer. **Test Plan:** Used test server from facebook#6889 (in `--binary` mode) to send some binary data to the Android UIExplorer example (also from facebook#6889). Verified it's received correctly as `ArrayBuffer`. Closes facebook#6868 Differential Revision: D3184797 Pulled By: mkonicek fb-gh-sync-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d fbshipit-source-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d
Summary:- Get rid of no longer necessary WebSocket.js v WebSocketBase.js split - Use `EventTarget(list, of, events)` as base class to auto-generate `oneventname` getters/setters that get invoked along with other event handlers - Type annotation `any` considered harmful, especially when we can easily spell out the actual type - Throw in some `const` goodness for free **Test Plan:** Launch UIExplorer example app, supplied `websocket_test_server` script, and try different combinations of sending and receiving text and binary data on both iOS and Android. Closes facebook#6889 Differential Revision: D3184835 Pulled By: mkonicek fb-gh-sync-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd fbshipit-source-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd
Summary:This is a reprise of facebook#6327, but with iOS 7.0 compatibility and less `package.json` changes. **Test Plan:** Load WebSocketExample in UIExplorer app and start websocket test server script (both provided in facebook#6889) and test sending binary data on both iOS and Android Closes facebook#6961 Differential Revision: D3202022 Pulled By: mkonicek fb-gh-sync-id: 38843d0a9c0172971c5c70a5139ded04042b280a fbshipit-source-id: 38843d0a9c0172971c5c70a5139ded04042b280a
Summary:This brings the same functionality that's already present on iOS, introduced in facebook#4483, to Android: convert binary payloads to base64 strings and send them to JS land that way, where they'll be turned into an ArrayBuffer. **Test Plan:** Used test server from facebook#6889 (in `--binary` mode) to send some binary data to the Android UIExplorer example (also from facebook#6889). Verified it's received correctly as `ArrayBuffer`. Closes facebook#6868 Differential Revision: D3184797 Pulled By: mkonicek fb-gh-sync-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d fbshipit-source-id: e78c640c43b3e41a75ddba79acc04e5eaab5667d
Summary:- Get rid of no longer necessary WebSocket.js v WebSocketBase.js split - Use `EventTarget(list, of, events)` as base class to auto-generate `oneventname` getters/setters that get invoked along with other event handlers - Type annotation `any` considered harmful, especially when we can easily spell out the actual type - Throw in some `const` goodness for free **Test Plan:** Launch UIExplorer example app, supplied `websocket_test_server` script, and try different combinations of sending and receiving text and binary data on both iOS and Android. Closes facebook#6889 Differential Revision: D3184835 Pulled By: mkonicek fb-gh-sync-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd fbshipit-source-id: f21707f4e97aa5a79847f5157e0a9f132a1a01cd
Summary:This is a reprise of facebook#6327, but with iOS 7.0 compatibility and less `package.json` changes. **Test Plan:** Load WebSocketExample in UIExplorer app and start websocket test server script (both provided in facebook#6889) and test sending binary data on both iOS and Android Closes facebook#6961 Differential Revision: D3202022 Pulled By: mkonicek fb-gh-sync-id: 38843d0a9c0172971c5c70a5139ded04042b280a fbshipit-source-id: 38843d0a9c0172971c5c70a5139ded04042b280a
Summary: **Motivation** This is originally a follow up of PR #6327 by dewe and jeremyong And mkonicek indicated this should split into a separate PR. Now philikon takes over #6327 as #6961 **Test Plan:** According to #6961 and #6889 Load WebSocketExample in UIExplorer app and start websocket test server script and test sending binary data on both iOS and Android Closes #6937 Reviewed By: javache Differential Revision: D3669596 Pulled By: mkonicek fbshipit-source-id: 342e29eb34de882bcbd9f297aab71dd6bb236748
EventTarget(list, of, events)
as base class to auto-generateoneventname
getters/setters that get invoked along with other event handlersany
considered harmful, especially when we can easily spell out the actual typeconst
goodness for freeTest Plan: Launch UIExplorer example app, supplied
websocket_test_server
script, and try different combinations of sending and receiving text and binary data on both iOS and Android.