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 <[email protected]> Authored: Tue Dec 26 11:10:07 2017 +0800 Committer: misakuo <[email protected]> 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); + } + } + } }
