Skip to content

Conversation

philikon
Copy link
Contributor

@philikon philikon commented Apr 8, 2016

  • 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.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @hharnisc, @satya164 and @aprock to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 8, 2016
@facebook-github-bot
Copy link
Contributor

@philikon updated the pull request.

@facebook-github-bot
Copy link
Contributor

@philikon updated the pull request.

@christopherdro
Copy link
Contributor

@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);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@satya164
Copy link
Contributor

Can you provide a test plan?

event = new WebSocketEvent('close');
event.message = ev.message;
this.dispatchEvent(event);

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: unnecessary new line

Copy link
Contributor Author

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.

Copy link
Contributor Author

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"?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@satya164
Copy link
Contributor

There's some inconsistency regarding where you pass the id and where you use this._socketId. For example, _unregisterEvents uses this._socketId and the id is passed to _closeWebSocket. Personally I like to pass things as an argument even if it's on the instance. Helps in refactoring since those methods stay pure, and also decreases the number of keypresses, (id vs this._socketId).

@philikon
Copy link
Contributor Author

There's some inconsistency regarding where you pass the id and where you use this._socketId. For example, _unregisterEvents uses this._socketId and the id is passed to _closeWebSocket.

Thanks for pointing that out. I'll fix that.

Personally I like to pass things as an argument even if it's on the instance. Helps in refactoring since those methods stay pure, and also decreases the number of keypresses, (id vs this._socketId).

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.)

@satya164
Copy link
Contributor

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.

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 socketId instead if id is not obvious to you. I just mentioned my preference and the reasons why I prefer it. But it's fine if you disagree.

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.)

I don't think there's any preferred style anywhere in the codebase. Looking at XMLHttpRequest, its written in a slightly different way, so it's not really applicable there.

- 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
@philikon
Copy link
Contributor Author

I've noticed you've been working on a couple stuff with Websockets lately

Yep, and more to come. I'm planning on picking up #6327 and getting it into shape to land.

and was just curious how your testing these new features and if you so on both iOS and Android?

Ah, yes. I have a little test script + app. I should just add those as a UIExplorer example!

@facebook-github-bot
Copy link
Contributor

@philikon updated the pull request.

};

_sendText = () => {
this.state.socket.send(this.state.outgoingMessage);

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

@facebook-github-bot
Copy link
Contributor

@philikon updated the pull request.

@mkonicek
Copy link
Contributor

This PR and #6961 both add the example, which one should be merged?

@mkonicek
Copy link
Contributor

Thanks for doing this! The test plan looks good. Shall we #shipit ?

@mkonicek
Copy link
Contributor

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@philikon
Copy link
Contributor Author

@mkonicek #6961 is just on top of my other PRs. Once they've landed, I will rebase #6961.

@philikon
Copy link
Contributor Author

What's blocking this from getting merged at this point?

ghost pushed a commit that referenced this pull request Apr 18, 2016
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
@mkonicek
Copy link
Contributor

mkonicek commented Apr 18, 2016

@philikon Failed landcastle with unrelated errors which is quite common :( Just hit 'Re-ship' for you and will keep an eye on the status.

@ghost ghost closed this in ebb44d2 Apr 18, 2016
@philikon
Copy link
Contributor Author

Woo, thanks!

@philikon philikon deleted the ws_cleanup branch April 19, 2016 00:20
ghost pushed a commit that referenced this pull request Apr 20, 2016
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
ptmt pushed a commit to ptmt/react-native that referenced this pull request May 9, 2016
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
ptmt pushed a commit to ptmt/react-native that referenced this pull request May 9, 2016
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
ptmt pushed a commit to ptmt/react-native that referenced this pull request May 9, 2016
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
rozele referenced this pull request in microsoft/react-native-windows May 25, 2016
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
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
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
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
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
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
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
bubblesunyum pushed a commit to iodine/react-native that referenced this pull request Aug 23, 2016
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
bubblesunyum pushed a commit to iodine/react-native that referenced this pull request Aug 23, 2016
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
bubblesunyum pushed a commit to iodine/react-native that referenced this pull request Aug 23, 2016
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
ghost pushed a commit that referenced this pull request Sep 20, 2016
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
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants