-
Notifications
You must be signed in to change notification settings - Fork 24.7k
[WebSocket][Android] make protocols argument work fixes #5810 #6223
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
NOTE: this PR does NOT change any api NOR add any new functionality. It is just only un-comment the original implementation which considered okHttp will swallow headers we added thus commented out the |
@zxcpoiu updated the pull request. |
StringBuilder protocolsValue = new StringBuilder(""); | ||
for (int i = 0; i < protocols.size(); i++) { | ||
String v = protocols.getString(i).trim(); | ||
if (!TextUtils.isEmpty(v) && !v.contains(",")) { |
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 not use String.isEmpty
?
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, it can be avoided to import additional TextUtils
.
Just a side note:
String.isEmpty()
only checks length of string and introduced since API level 9 (Android 2.3)
so it maybe caused some crashes on earlier versions of Android.
TextUtils.isEmpty()
checks both length of string and null.
is available since API level 1
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.
@zxcpoiu It's okay, because we only support Android 4.1 and above.
Related #6016 |
@zxcpoiu I'm wondering if my PR thats linked here will work since it allows you to specify custom headers. |
thanks for the great PR, it really helps! But I think the reason that original WebSocket implementation left an argument So I guess it is related PR but not overlaps. Original implementation protocols argument |
@zxcpoiu updated the pull request. |
I've refined the PR by:
diff about this change diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java
index ea2329a..af4f092 100644
--- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java
+++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java
@@ -38,8 +38,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
-import android.text.TextUtils;
-
import okio.Buffer;
import okio.BufferedSource;
@@ -91,12 +89,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
StringBuilder protocolsValue = new StringBuilder("");
for (int i = 0; i < protocols.size(); i++) {
String v = protocols.getString(i).trim();
- if (!TextUtils.isEmpty(v) && !v.contains(",")) {
+ if (!v.isEmpty() && !v.contains(",")) {
protocolsValue.append(v);
protocolsValue.append(",");
}
}
- if (!TextUtils.isEmpty(protocolsValue)) {
+ if (protocolsValue.length() > 0) {
protocolsValue.replace(protocolsValue.length() - 1, protocolsValue.length(), "");
builder.addHeader("Sec-WebSocket-Protocol", protocolsValue.toString());
} |
@zxcpoiu Just a heads up that my related PR just got shipped. |
@christopherdro |
fb80688
to
d687c4a
Compare
@zxcpoiu updated the pull request. |
fixed conflicts follow up #6016 |
Hi @satya164 |
@christopherdro Do you think this is good to go? |
satya This looks good to me. Just needs to be rebased to master. |
thanks @satya164 @christopherdro |
286e633
to
6999fe2
Compare
@zxcpoiu updated the pull request. |
@facebook-github-bot shipit |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
Summary:Hey there and thanks for submitting a pull request! Please have a look at the following checklist so that others have enough information to review your pull request: **motivation** WebSocket spec supports [Sec-WebSocket-Protocol](https://tools.ietf.org/html/rfc6455#section-11.3.4) as a standard way for negotiate a sub protocol between client and server. * ios WebSocket implementation supports it. * android WebSocket implementation ignores this header, leave a comment syas: "OkHttp will overrides it", so it did not implement. * after some test, OkHttp doesn't override the header we add. **Test plan (required)** 1. run and react-native app on android 2. at the main page, invoke: `var ws = new WebSocket('ws://example.ws-service.fakedomain.com', 'my-sub-protocol');` 3. see the header if it send the correct header, ex, use ngrep: `sudo ngrep -t -Wbyline -deth0 host example.ws-service.fakedomain.com and port 80` you should see the WebSocket initial GET handshake includes header: `Sec-WebSocke Closes facebook#6223 Differential Revision: D3162822 fb-gh-sync-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264 fbshipit-source-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264
Summary:Hey there and thanks for submitting a pull request! Please have a look at the following checklist so that others have enough information to review your pull request: **motivation** WebSocket spec supports [Sec-WebSocket-Protocol](https://tools.ietf.org/html/rfc6455#section-11.3.4) as a standard way for negotiate a sub protocol between client and server. * ios WebSocket implementation supports it. * android WebSocket implementation ignores this header, leave a comment syas: "OkHttp will overrides it", so it did not implement. * after some test, OkHttp doesn't override the header we add. **Test plan (required)** 1. run and react-native app on android 2. at the main page, invoke: `var ws = new WebSocket('ws://example.ws-service.fakedomain.com', 'my-sub-protocol');` 3. see the header if it send the correct header, ex, use ngrep: `sudo ngrep -t -Wbyline -deth0 host example.ws-service.fakedomain.com and port 80` you should see the WebSocket initial GET handshake includes header: `Sec-WebSocke Closes facebook#6223 Differential Revision: D3162822 fb-gh-sync-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264 fbshipit-source-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264
Hey there and thanks for submitting a pull request! Please have a look at the following checklist so that others have enough information to review your pull request:
motivation
WebSocket spec supports Sec-WebSocket-Protocol as a standard way for negotiate a sub protocol between client and server.
Test plan (required)
var ws = new WebSocket('ws://example.ws-service.fakedomain.com', 'my-sub-protocol');
sudo ngrep -t -Wbyline -deth0 host example.ws-service.fakedomain.com and port 80
you should see the WebSocket initial GET handshake includes header:
Sec-WebSocket-Protocol: my-sub-protocol
and the value should equals to the value you provided in js.
Closes #5810