Giter Club home page Giter Club logo

Comments (5)

dhhAndroid avatar dhhAndroid commented on July 21, 2024

这个是依托于RxJava写的,内部的WebSocket是共享的,所以当 当前的Observable取消订阅的时候,就是WebSocket关闭的时候,所以回调的WebSocketSubscriber 中的onClose方法,当当前url对应的WebSocket没有Observable订阅者的时候,内部的WebSocket就关闭了。所以WebSocketSubscriber 中的onClose方法相当于当前是当前Observable取消订阅的回调。

from rxwebsocket.

ZDZN avatar ZDZN commented on July 21, 2024

其实你说的在取消订阅时回调 onClose 这种情况我也想过,但是在我自己写的一个测试例子里,调用 stopService 之后(在这里是已经取消订阅了)并没有执行 onClose 这个方法。
下面测试的例子,Activity 中只有两个按钮,一个是调用 serviceStart,一个是调用 serviceStop 方法

public class NotificationService extends Service {
    public static void serviceStart(Context context) {
        Intent intent = new Intent(context, NotificationService.class);
        context.startService(intent);
    }

    public static void serviceStop(Context context) {
        Intent intent = new Intent(context, NotificationService.class);
        context.stopService(intent);
    }

    private static final String TAG = "NotificationService";
    // 网上的服务器
    private static final String URL_WEB_SOCKET = "ws://121.40.165.18:8800";

    private WebSocketSubscriber mWebSocketSubscriber;

    @Override
    public void onCreate() {
        super.onCreate();
        Config config = new Config.Builder().setShowLog(true).setReconnectInterval(5, TimeUnit.SECONDS).build();
        RxWebSocket.setConfig(config);
        mWebSocketSubscriber = new WebSocketSubscriber() {
            @Override
            protected void onOpen(WebSocket webSocket) {
                Log.d(TAG, "RxWebSocket opened");
            }

            @Override
            protected void onMessage(String text) {
                Log.d(TAG, "RxWebSocket message: " + text);
            }

            @Override
            protected void onReconnect() {
                Log.d(TAG, "RxWebSocket reconnect");
            }

            @Override
            protected void onClose() {
                Log.d(TAG, "RxWebSocket close");
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "RxWebSocket error", e);
            }
        };
        Log.d(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        RxWebSocket.get(URL_WEB_SOCKET).subscribe(mWebSocketSubscriber);
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        mWebSocketSubscriber.dispose();
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}

from rxwebsocket.

dhhAndroid avatar dhhAndroid commented on July 21, 2024

嗯,谢谢你,问题我找到了,源码里在注销的时候,我没有调用onComplete,所以在注销的时候,没有回调onClose,RxWebSocket内部的日志,已经显示关闭WebSocket了。日志如下:
D/RxWebSocket: ws://121.40.165.18:8800 --> onOpen
D/RxWebSocket: OnDispose
D/RxWebSocket: ws://121.40.165.18:8800 --> cancel

from rxwebsocket.

dhhAndroid avatar dhhAndroid commented on July 21, 2024

如果你要在WebSocket关闭的时候做事情,你暂时可以使用doOnDispose操作符这样做,可以实现同样效果(这是kotlin代码,你用Java写就行了):

        RxWebSocket.get("ws://121.40.165.18:8800")
                .doOnDispose { "相当于onClose回调" }
                .subscribe(mWebSocketSubscriber)

from rxwebsocket.

ZDZN avatar ZDZN commented on July 21, 2024

谢谢

from rxwebsocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.