-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Conversation
@@ -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" |
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.
Since we already include okhttp-ws
a few lines above this, we should use that instead of adding another dependency.
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.
Thanks, I'll work on migrating the code to use okhttp ws.
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.
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.
Thanks for the pull request! Please see my note about using |
Regarding tests, we have not opensourced those yet so we don't have a story around it. Stay tuned! (cc @mkonicek) |
@foghina Thanks. looking forward to it :) |
@foghina From the README on the okhttp-ws page
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 |
Yes please migrate to See how it's used here: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/JSDebuggerWebSocketClient.java |
@mkonicek Ok, I'll migrate to okhttp-ws and update the pull request accordingly. |
Pull request updated. Mostly changes from #2870 |
|
||
if (mWebSocketClient != null) { | ||
try { | ||
mWebSocketClient.close(1000, ""); |
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.
why 1000?
nvm: can you add a comment or assign this to a variable/constant so it's documented?
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? |
@ide The status code 1000 means I added the comments. |
@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. |
@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. |
@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). |
Any updates on this? |
this.onopen && this.onopen(); | ||
}), | ||
RCTDeviceEventEmitter.addListener('websocketClosed', ev => { | ||
if (ev.id !== id) { |
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: can you indent only 2 spaces
I'm haven't yet tried the shipit bot on PRs where the commits haven't been squashed. Let's see what happens :) |
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. |
@mkonicek probably this. |
Import looks good, except it failed because |
Just send a separate PR for the docs please. |
Actually let me do that. |
|
||
'use strict'; | ||
|
||
var Event = require('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.
Just curious - this is quite a generic name and could conflict with other code in the future. How about calling it WebSocketEvent
?
@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 |
Thanks for the explanation! I think |
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. |
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 |
@mkonicek Awesome. Thanks a lot :D |
@mkonicek Any updates on this? |
I had to update the Java files a bit, merging now.. |
This got delayed because it's failing some internal iOS tests. Looking. |
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 :) |
@mkonicek Thanks a lot for keeping me updated. Much appreciated :D |
No problem ☺ |
f4857a6
Yay! |
Awesome! |
@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: 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. |
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. |
@mkonicek No problem. Yeah, sure, I can do the work next time :D This was a really small commit though, so it's alright :) |
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
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
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
.