Skip to content
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

如何短时间内手动关闭websocket连接 #29

Closed
JerryChaox opened this issue Aug 16, 2018 · 3 comments
Closed

如何短时间内手动关闭websocket连接 #29

JerryChaox opened this issue Aug 16, 2018 · 3 comments

Comments

@JerryChaox
Copy link

有时候虽然绑定了Activity的生命周期,但是有时候希望在Activity生命周期内的某一段时间暂时关闭websocket链接

@JerryChaox JerryChaox changed the title 如何手动关闭websocket链接 如何短时间内手动关闭websocket连接 Aug 16, 2018
@dhhAndroid
Copy link
Owner

你用的RxJava1,还是RxJava2版本?

@wolfking0608
Copy link

com.dhh:websocket2:2.1.4'如何实现手动取消连接,别自动连接
这种: 有些需求要手动关闭它
Config config = new Config.Builder().setClient(new OkHttpClient().newBuilder()
.pingInterval(2, TimeUnit.SECONDS).build()
).setShowLog(true, "test_websocket").build();
RxWebSocket.setConfig(config);
//处理有时候,加载过慢,url为空闪退问题
webSocketInfoObservable = RxWebSocket.get("wss://chattest.xxxxx.com");
webSocketInfoObservable.as(RxLifecycleUtils.bindLifecycle(getLifecycleOwner()))
.subscribe(new WebSocketSubscriber() {

                @Override
                protected void onMessage(String messageInfo) {
                    Log.e("messageInfo", "messageInfo==" + messageInfo);
                    onMessageResponse(messageInfo);
                }

                @Override
                protected void onMessage(ByteString byteString) {
                    super.onMessage(byteString);
                    Log.e("messageInfo", "byteString==" + byteString.toString());
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    Log.e("messageInfo", "onError==" + e.getMessage());
                }

                @Override
                protected void onOpen(WebSocket webSocket) {
                    mWebSocket = webSocket;
                    super.onOpen(webSocket);

           
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("type", "ping");
                    WebSocketSetting.setConnectUrl("wss://chattest.xxx.com");
                    RxWebSocket.send(WebSocketSetting.getConnectUrl(), jsonObject.toJSONString());
                    MLog.e("-------------------------------", "ping-------------------------");
                    initDownTimer();
                }

                @Override
                protected void onReconnect() {
                    super.onReconnect();
                    Log.e("messageInfo", "onReconnect");
                }

                @Override
                protected void onClose() {
                    super.onClose();
                    Log.e("messageInfo", "onClose");
                }
            });

@dhhAndroid
Copy link
Owner

请查看 issues #50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants