You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by mi...@apache.org on 2018/02/08 07:37:22 UTC

[1/3] incubator-weex git commit: * [android] bind ws callback to it's event listener

Repository: incubator-weex
Updated Branches:
  refs/heads/master 14b294d50 -> 1605d6601


* [android] bind ws callback to it's event listener


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/592c3405
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/592c3405
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/592c3405

Branch: refs/heads/master
Commit: 592c3405bcaecc1c9fc08ea4c3df3e61cf04f382
Parents: a45d51b
Author: misakuo <mi...@apache.org>
Authored: Tue Dec 26 11:10:07 2017 +0800
Committer: misakuo <mi...@apache.org>
Committed: Tue Dec 26 11:10:07 2017 +0800

----------------------------------------------------------------------
 .../weex/appfram/websocket/WebSocketModule.java | 102 +++++++++++--------
 1 file changed, 57 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/592c3405/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java b/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
index e2da43b..8e6d3b2 100644
--- a/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
+++ b/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
@@ -39,47 +39,7 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
     private static final String KEY_WAS_CLEAN = "wasClean";
 
     private IWebSocketAdapter webSocketAdapter;
-    private JSCallback onOpen;
-    private JSCallback onMessage;
-    private JSCallback onClose;
-    private JSCallback onError;
-    private IWebSocketAdapter.EventListener eventListener = new IWebSocketAdapter.EventListener() {
-        @Override
-        public void onOpen() {
-            if (onOpen != null) {
-                onOpen.invoke(new HashMap<>(0));
-            }
-        }
-
-        @Override
-        public void onMessage(String data) {
-            if (onMessage != null) {
-                Map<String, String> msg = new HashMap<>(1);
-                msg.put(KEY_DATA, data);
-                onMessage.invokeAndKeepAlive(msg);
-            }
-        }
-
-        @Override
-        public void onClose(int code, String reason, boolean wasClean) {
-            if (onClose != null) {
-                Map<String, Object> msg = new HashMap<>(3);
-                msg.put(KEY_CODE, code);
-                msg.put(KEY_REASON, reason);
-                msg.put(KEY_WAS_CLEAN, wasClean);
-                onClose.invoke(msg);
-            }
-        }
-
-        @Override
-        public void onError(String msg) {
-            if (onError != null) {
-                Map<String, String> info = new HashMap<>(1);
-                info.put(KEY_DATA, msg);
-                onError.invokeAndKeepAlive(info);
-            }
-        }
-    };
+    private WebSocketEventListener eventListener;
 
     @JSMethod
     public void WebSocket(String url, String protocol) {
@@ -88,6 +48,7 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
         }
         webSocketAdapter = mWXSDKInstance.getWXWebSocketAdapter();
         if (!reportErrorIfNoAdapter()) {
+            eventListener = new WebSocketEventListener();
             webSocketAdapter.connect(url, protocol, eventListener);
         }
     }
@@ -116,22 +77,30 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
 
     @JSMethod
     public void onopen(JSCallback callback) {
-        this.onOpen = callback;
+        if (eventListener != null) {
+            eventListener.onOpen = callback;
+        }
     }
 
     @JSMethod
     public void onmessage(JSCallback callback) {
-        this.onMessage = callback;
+        if (eventListener != null) {
+            eventListener.onMessage = callback;
+        }
     }
 
     @JSMethod
     public void onclose(JSCallback callback) {
-        this.onClose = callback;
+        if (eventListener != null) {
+            eventListener.onClose = callback;
+        }
     }
 
     @JSMethod
     public void onerror(JSCallback callback) {
-        this.onError = callback;
+        if (eventListener != null) {
+            eventListener.onError = callback;
+        }
     }
 
     @Override
@@ -152,4 +121,47 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
         }
         return false;
     }
+
+    private class WebSocketEventListener implements IWebSocketAdapter.EventListener {
+        private JSCallback onOpen;
+        private JSCallback onClose;
+        private JSCallback onError;
+        private JSCallback onMessage;
+
+        @Override
+        public void onOpen() {
+            if (onOpen != null) {
+                onOpen.invoke(new HashMap<>(0));
+            }
+        }
+
+        @Override
+        public void onMessage(String data) {
+            if (onMessage != null) {
+                Map<String, String> msg = new HashMap<>(1);
+                msg.put(KEY_DATA, data);
+                onMessage.invokeAndKeepAlive(msg);
+            }
+        }
+
+        @Override
+        public void onClose(int code, String reason, boolean wasClean) {
+            if (onClose != null) {
+                Map<String, Object> msg = new HashMap<>(3);
+                msg.put(KEY_CODE, code);
+                msg.put(KEY_REASON, reason);
+                msg.put(KEY_WAS_CLEAN, wasClean);
+                onClose.invoke(msg);
+            }
+        }
+
+        @Override
+        public void onError(String msg) {
+            if (onError != null) {
+                Map<String, String> info = new HashMap<>(1);
+                info.put(KEY_DATA, msg);
+                onError.invokeAndKeepAlive(info);
+            }
+        }
+    }
 }


[3/3] incubator-weex git commit: Merge remote-tracking branch 'upstream/pr1015' into apache-master

Posted by mi...@apache.org.
Merge remote-tracking branch 'upstream/pr1015' into apache-master


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/1605d660
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/1605d660
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/1605d660

Branch: refs/heads/master
Commit: 1605d66019139a70de11091e9c90dd0078c34fb9
Parents: 14b294d 78a6e67
Author: misakuo <mi...@apache.org>
Authored: Thu Feb 8 15:37:04 2018 +0800
Committer: misakuo <mi...@apache.org>
Committed: Thu Feb 8 15:37:04 2018 +0800

----------------------------------------------------------------------
 .../weex/appfram/websocket/WebSocketModule.java | 141 ++++++++++++-------
 1 file changed, 87 insertions(+), 54 deletions(-)
----------------------------------------------------------------------



[2/3] incubator-weex git commit: * [android] moving network operating to async thread

Posted by mi...@apache.org.
* [android] moving network operating to async thread


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/78a6e676
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/78a6e676
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/78a6e676

Branch: refs/heads/master
Commit: 78a6e67659fca8d83eb6d8e2fad5e13bd8a36a1a
Parents: 592c340
Author: misakuo <mi...@apache.org>
Authored: Thu Dec 28 15:27:32 2017 +0800
Committer: misakuo <mi...@apache.org>
Committed: Thu Dec 28 15:27:32 2017 +0800

----------------------------------------------------------------------
 .../weex/appfram/websocket/WebSocketModule.java | 41 +++++++++++++++-----
 1 file changed, 31 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/78a6e676/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java b/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
index 8e6d3b2..f05f560 100644
--- a/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
+++ b/android/sdk/src/main/java/com/taobao/weex/appfram/websocket/WebSocketModule.java
@@ -18,9 +18,12 @@
  */
 package com.taobao.weex.appfram.websocket;
 
+import android.os.Looper;
+
 import com.taobao.weex.WXSDKEngine;
 import com.taobao.weex.annotation.JSMethod;
 import com.taobao.weex.bridge.JSCallback;
+import com.taobao.weex.bridge.WXBridgeManager;
 import com.taobao.weex.utils.WXLogUtils;
 
 import java.util.HashMap;
@@ -41,9 +44,14 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
     private IWebSocketAdapter webSocketAdapter;
     private WebSocketEventListener eventListener;
 
-    @JSMethod
+    public WebSocketModule() {
+        WXLogUtils.e(TAG, "create new instance");
+    }
+
+    @JSMethod(uiThread = false)
     public void WebSocket(String url, String protocol) {
         if (webSocketAdapter != null) {
+            WXLogUtils.w(TAG, "close");
             webSocketAdapter.close(WebSocketCloseCodes.CLOSE_GOING_AWAY.getCode(), WebSocketCloseCodes.CLOSE_GOING_AWAY.name());
         }
         webSocketAdapter = mWXSDKInstance.getWXWebSocketAdapter();
@@ -53,14 +61,14 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
         }
     }
 
-    @JSMethod
+    @JSMethod(uiThread = false)
     public void send(String data) {
         if (!reportErrorIfNoAdapter()) {
             webSocketAdapter.send(data);
         }
     }
 
-    @JSMethod
+    @JSMethod(uiThread = false)
     public void close(String code, String reason) {
         if (!reportErrorIfNoAdapter()) {
             int codeNumber = WebSocketCloseCodes.CLOSE_NORMAL.getCode();
@@ -75,28 +83,28 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
         }
     }
 
-    @JSMethod
+    @JSMethod(uiThread = false)
     public void onopen(JSCallback callback) {
         if (eventListener != null) {
             eventListener.onOpen = callback;
         }
     }
 
-    @JSMethod
+    @JSMethod(uiThread = false)
     public void onmessage(JSCallback callback) {
         if (eventListener != null) {
             eventListener.onMessage = callback;
         }
     }
 
-    @JSMethod
+    @JSMethod(uiThread = false)
     public void onclose(JSCallback callback) {
         if (eventListener != null) {
             eventListener.onClose = callback;
         }
     }
 
-    @JSMethod
+    @JSMethod(uiThread = false)
     public void onerror(JSCallback callback) {
         if (eventListener != null) {
             eventListener.onError = callback;
@@ -105,10 +113,23 @@ public class WebSocketModule extends WXSDKEngine.DestroyableModule {
 
     @Override
     public void destroy() {
-        if (webSocketAdapter != null) {
-            webSocketAdapter.destroy();
+        Runnable destroyTask = new Runnable() {
+            @Override
+            public void run() {
+                WXLogUtils.w(TAG, "close session with instance id " + mWXSDKInstance.getInstanceId());
+                if (webSocketAdapter != null) {
+                    webSocketAdapter.destroy();
+                }
+                webSocketAdapter = null;
+                eventListener = null;
+            }
+        };
+
+        if (Looper.myLooper() == Looper.getMainLooper()) {
+            WXBridgeManager.getInstance().post(destroyTask);
+        } else {
+            destroyTask.run();
         }
-        eventListener = null;
     }
 
     private boolean reportErrorIfNoAdapter() {