Skip to content

Commit

Permalink
fix(Android): web socket client boolean to AtomicBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli authored and zoomchan-cxj committed Jul 25, 2022
1 parent 31c4cf5 commit c267f9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class UIManagerModule extends HippyNativeModuleBase {
final String NAME = "name";
final String PROPS = "props";
final String TAG_NAME = "tagName";
final String NATIVE_NAME = "nativeName";

public UIManagerModule(HippyEngineContext context) {
super(context);
Expand All @@ -67,11 +66,6 @@ public void createNode(int rootID, HippyArray hippyArray) {
}

String className = (String) nodeArray.get(NAME);
String nativeName = nodeArray.getString(NATIVE_NAME);
if (!TextUtils.isEmpty(nativeName) && !TextUtils.isEmpty(className)
&& !TextUtils.equals(nativeName, className)) {
className = nativeName;
}
String tagName = (String) nodeArray.get(TAG_NAME);
HippyMap props = (HippyMap) nodeArray.get(PROPS);
domManager.createNode(hippyRootView, rootID, tag, pTag, index, className, tagName, props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@SuppressWarnings({"unused", "FieldCanBeLocal"})
public class WebSocketClient {
Expand All @@ -53,7 +54,7 @@ public class WebSocketClient {
private final Handler mHandler;
private final List<Header> mExtraHeaders;
private final HybiParser mParser;
private boolean mConnected;
private AtomicBoolean mConnected;
public static final String DISCONNECT_REASON_EOF = "EOF";
public static final String DISCONNECT_REASON_SSL = "SSL";
public static final String DISCONNECT_REASON_CONNECT = "CONNECT";
Expand All @@ -63,7 +64,7 @@ public WebSocketClient(URI uri, WebSocketListener listener, List<Header> extraHe
mURI = uri;
mListener = listener;
mExtraHeaders = extraHeaders;
mConnected = false;
mConnected = new AtomicBoolean(false);
mParser = new HybiParser(this);

mHandlerThread = new HandlerThread("websocket-thread");
Expand Down Expand Up @@ -151,30 +152,29 @@ public void run() {
}

mListener.onConnect();

mConnected = true;
mConnected.set(true);

// Now decode websocket frames.
mParser.start(stream);

} catch (EOFException ex) {
Log.d(TAG, "WebSocket EOF!", ex);
mListener.onDisconnect(0, DISCONNECT_REASON_EOF);
mConnected = false;
mConnected.set(false);
} catch (SSLException ex) {
// Connection reset by peer
Log.d(TAG, "Websocket SSL error!", ex);
mListener.onDisconnect(0, DISCONNECT_REASON_SSL);
mConnected = false;
mConnected.set(false);
} catch (ConnectException ex) {
// WebSocketClient received no reply from server.
Log.d(TAG, "Websocket Connect error!", ex);
mListener.onDisconnect(0, DISCONNECT_REASON_CONNECT);
mConnected = false;
mConnected.set(false);
} catch (Throwable ex) {
mListener.onError(new Exception(ex));
} finally {
if (!mConnected && mSocket != null) {
if (!mConnected.get() && mSocket != null) {
try {
mSocket.close();
} catch (Throwable ex) {
Expand Down Expand Up @@ -203,7 +203,7 @@ public void run() {
mListener.onDisconnect(0, DISCONNECT_REASON_CLOSE);
mSocket = null;
}
mConnected = false;
mConnected.set(false);
}
});
}
Expand All @@ -223,7 +223,7 @@ public void requestClose(int code, String reason) {
}

public boolean isConnected() {
return mConnected;
return mConnected.get();
}

private StatusLine parseStatusLine(String line) throws IOException {
Expand Down

0 comments on commit c267f9e

Please sign in to comment.