Skip to content
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

Implement WebSocket module for Android. Fixes #2837 #2839

Closed
wants to merge 5 commits into from
Closed

Implement WebSocket module for Android. Fixes #2837 #2839

wants to merge 5 commits into from

Conversation

satya164
Copy link
Contributor

The JavaScript code for Android is same as the iOS counterpart, I just added few new lines and used arrow functions instead of binding this.

@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 Sep 18, 2015
@@ -240,6 +240,7 @@ dependencies {
compile 'com.squareup.okhttp:okhttp-ws:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'
compile 'org.webkit:android-jsc:r174650'
compile "org.java-websocket:Java-WebSocket:1.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we already include okhttp-ws a few lines above this, we should use that instead of adding another dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll work on migrating the code to use okhttp ws.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with @foghina's suggestion. If you find that Java-WebSocket works better though you could always publish your own module to npm instead though.

@foghina
Copy link
Contributor

foghina commented Sep 18, 2015

Thanks for the pull request!

Please see my note about using okhttp-ws instead of org.java-websocket. Which means WebSocketModule will need to be changed as well. But the general approach seems fine.

@foghina
Copy link
Contributor

foghina commented Sep 18, 2015

Regarding tests, we have not opensourced those yet so we don't have a story around it. Stay tuned! (cc @mkonicek)

@satya164
Copy link
Contributor Author

@foghina Thanks. looking forward to it :)

@satya164
Copy link
Contributor Author

@foghina From the README on the okhttp-ws page

Note: This module's API should be considered experimental and may be subject to breaking changes in future releases.

Currently it's used to connect to Chrome debugger I think. The docs don't have anything and the API is also subject to change. Should I migrate it to use okhttp-ws ? I'm a bit skeptical.

@mkonicek
Copy link
Contributor

Yes please migrate to okhttp-ws, if there are breaking changes we'll migrate.

See how it's used here: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/JSDebuggerWebSocketClient.java

@satya164
Copy link
Contributor Author

@mkonicek Ok, I'll migrate to okhttp-ws and update the pull request accordingly.

@satya164
Copy link
Contributor Author

Pull request updated. Mostly changes from #2870


if (mWebSocketClient != null) {
try {
mWebSocketClient.close(1000, "");
Copy link
Contributor

Choose a reason for hiding this comment

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

why 1000?

nvm: can you add a comment or assign this to a variable/constant so it's documented?

@ide
Copy link
Contributor

ide commented Sep 20, 2015

I really want to share more JS between iOS and Android. My thinking is that we should merge #2599 first, to get iOS's implementation even better, and then start moving code out of WebSocket.ios.js into WebSocketBase.js till only a few lines need to be specific to iOS or Android.

@mkonicek can you do a pass over the Java code style?

@satya164
Copy link
Contributor Author

@ide The status code 1000 means CLOSE_NORMAL.

I added the comments.

@satya164
Copy link
Contributor Author

@ide Also, the JavaScript code of both iOS and Android implementations are exactly the same. So when the pull request is merged, we can just copy/paste the code.

@ide
Copy link
Contributor

ide commented Sep 20, 2015

@satya164 If the code is exactly the same then I'm proposing that we rename WebSocket.ios.js to WebSocket.js and delete the platform-specific files.

@satya164
Copy link
Contributor Author

@ide Okay, so I was looking at the implementations and noticed that the close method didn't accept any parameters as per the spec. I changed the Android code so that it now accepts a code and a reason as argument, and the code is no longer the same :(

I'll look at the pull request for iOS and see if I can do those changes in my pull request for Android also (after it is merged).

@satya164
Copy link
Contributor Author

Any updates on this?

@satya164
Copy link
Contributor Author

@foghina @ide Would love to get this merged!

@satya164 satya164 mentioned this pull request Oct 1, 2015
this.onopen && this.onopen();
}),
RCTDeviceEventEmitter.addListener('websocketClosed', ev => {
if (ev.id !== id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you indent only 2 spaces

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

I'm haven't yet tried the shipit bot on PRs where the commits haven't been squashed. Let's see what happens :)

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/1661554830752790/int_phab to review.

@foghina
Copy link
Contributor

foghina commented Oct 2, 2015

@mkonicek probably this.

boom

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

Import looks good, except it failed because Network.md doesn't exist in the fb codebase - it is github only. Can you please remove the Network.md from this PR? We can edit it directly on github once this lands. We should probably also only update the docs once this gets released (to npm and Maven Central) which is when people can start to use it.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

Just send a separate PR for the docs please.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

Actually let me do that.


'use strict';

var Event = require('Event');
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious - this is quite a generic name and could conflict with other code in the future. How about calling it WebSocketEvent?

@satya164
Copy link
Contributor Author

satya164 commented Oct 2, 2015

@mkonicek I made the same changes as 626b551

I had to move those into separate files since we had 2 different files for iOS and Android. I believe the pull request was to provide a proper Event class. If I change the name it will be no longer be the case.

It's generic enough though, and not tied to WebSockets in any ways. So it could be used for other events also.

But if you want, I could inline them to the WebSocket.js file, so it'll not conflict with other files.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

Thanks for the explanation! I think Event is fine for now. The module system we use internally at fb uses global names so the websocket Event will be available anywhere using require('Event'), that's why I was suggesting require('WebSocketEvent').

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

I'll work on merging this internally now, need to make some changes to the Buck build scripts we use internally to build React Native.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 2, 2015

Still working on merging, will continue on Monday. No need to make more changes to the PR, looks good now. I'll just inline the Event into MessageEvent as Event is going to conflict with a class of the same name we have on the web.

@satya164
Copy link
Contributor Author

satya164 commented Oct 2, 2015

@mkonicek Awesome. Thanks a lot :D

@satya164
Copy link
Contributor Author

satya164 commented Oct 6, 2015

@mkonicek Any updates on this?

@mkonicek
Copy link
Contributor

mkonicek commented Oct 6, 2015

I had to update the Java files a bit, merging now..

@mkonicek
Copy link
Contributor

mkonicek commented Oct 6, 2015

This got delayed because it's failing some internal iOS tests. Looking.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 7, 2015

Will have to rebase once the fb internal tests are fixed and commit then. Hang tight! Thanks for the hard work, I literally just need to get this into the internal fb repo now :)

@satya164
Copy link
Contributor Author

satya164 commented Oct 7, 2015

@mkonicek Thanks a lot for keeping me updated. Much appreciated :D

@mkonicek
Copy link
Contributor

mkonicek commented Oct 7, 2015

No problem ☺

@ghost ghost closed this in f4857a6 Oct 7, 2015
@vjeux
Copy link
Contributor

vjeux commented Oct 7, 2015

Yay!

@satya164
Copy link
Contributor Author

satya164 commented Oct 7, 2015

Awesome!

mkonicek pushed a commit that referenced this pull request Oct 7, 2015
@mkonicek
Copy link
Contributor

mkonicek commented Oct 7, 2015

@satya164 The PR had to be split into two commits because of a technical detail of how the sync with the internal fb codebase works: Network.md lives on github only, all the other files also live in the fb codebase. Currently we have to commit these two types of changes separately.

I committed the docs for you which saves you a bit of work but makes the comit blame to me: c819c55

Let me know if you'd like to split the PR into two yourself next time and keep attribution that way.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 7, 2015

Long term we should probably have a way to auto-split the PR, don't have enough context on github APIs yet to estimate how much work that would be.

@satya164
Copy link
Contributor Author

satya164 commented Oct 7, 2015

@mkonicek No problem. Yeah, sure, I can do the work next time :D

This was a really small commit though, so it's alright :)

MattFoley pushed a commit to skillz/react-native that referenced this pull request Nov 9, 2015
Summary: The JavaScript code for Android is same as the iOS counterpart, I just added few new lines and used arrow functions instead of binding `this`.
Closes facebook#2839

Reviewed By: @​svcscm, @vjeux

Differential Revision: D2498703

Pulled By: @mkonicek

fb-gh-sync-id: 3fe958dd5af0efba00df07515f8e33b5d87eb05b
MattFoley pushed a commit to skillz/react-native that referenced this pull request Nov 9, 2015
Crash-- pushed a commit to Crash--/react-native that referenced this pull request Dec 24, 2015
Summary: The JavaScript code for Android is same as the iOS counterpart, I just added few new lines and used arrow functions instead of binding `this`.
Closes facebook#2839

Reviewed By: @​svcscm, @vjeux

Differential Revision: D2498703

Pulled By: @mkonicek

fb-gh-sync-id: 3fe958dd5af0efba00df07515f8e33b5d87eb05b
Crash-- pushed a commit to Crash--/react-native that referenced this pull request Dec 24, 2015
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