简体   繁体   English

RxJava:观察从套接字发出的消息

[英]RxJava: Observing messages emitted from a socket

I'm still trying to learn RxJava. 我还在努力学习RxJava。 There's one thing that I just can't wrap my head around right now. 有一件事我现在无法绕过头脑。 Every article that has tried to learn me how to use Rx has shown me how to create Observables based on sources that are already "predictable", ie sequences of a set amount of items (A single value or, for example, a simple Iterable). 每一篇试图了解我如何使用Rx的文章都向我展示了如何基于已经“可预测”的源创建Observable,即一组项目的序列(单个值,或者,例如,一个简单的Iterable) 。

Mostly you'll see something along the lines of Observable.just() 大多数情况下,你会看到Observable.just()

Observable<String> observerable = Observable.just("Hello, world!");

Or Observable.from() : 或者Observable.from()

Observable.from("apple", "orange", "banana").subscribe(fruit -> System.out.println(fruit));

That's all nice, but what about the following usecase? 这一切都很好,但是下面的用例呢?

I have messages that are constantly pushed through a socket (I didn't build it, I'm merely integrating). 我有不断通过套接字推送的消息(我没有构建它,我只是集成)。 I need to "Observe" the sequence of data that is being pushed through the socket. 我需要“观察”通过套接字推送的数据序列。

Many people seem to point to Obserable.using() ( Here's an example ), but I don't think it's the right solution either. 许多人似乎都指向Obserable.using()这是一个例子 ),但我认为这也不是正确的解决方案。 Messages pushed through the socket are incomplete because they have a maximum length. 通过套接字推送的消息不完整,因为它们具有最大长度。 I need to 'frame' the messages myself, so I need to buffer between each push from the socket. 我需要自己“构建”消息,因此我需要在每次从套接字推送之间进行缓冲。

In other words, I'm looking for a way to frame the messages myself from the data pushed from the socket and then push them into the Observable . 换句话说, 我正在寻找一种方法来自从套接字推送的数据构建消息,然后将它们推入Observable I've been looking for the proper way to do this all over the place, but I can't seem to find a proper solution. 我一直在寻找适当的方法来做到这一点,但我似乎找不到合适的解决方案。

What about Observable with fully customizable behavior? 具有完全可定制行为的Observable怎么样?

Observable.create(subscriber -> {
  Socket socket = getSocket();
  socket.subscribe(new SocketListener() {

    @Override public void onNewFrame(Frame frame) {
      // Process frame and prepare payload to the subscriber.

      if (payloadReadyForExternalObserver) {
        if (subscriber.isUnsubscribed()) {
          // Subscriber unsubscribed, let's close the socket.
          socket.close();
        } else {
          subscriber.onNext(payload);
        }
      }
    }

    @Override public void onSocketError(IOException exception) {
      subscriber.onError(exception); // Terminal state.
    }

    @Override public void onSocketClosed() {
      subscriber.onCompleted(); // Terminal state.
    }
  });
})

But make sure you implement Observable contract correctly, for more info please read https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators 但请确保正确实施Observable合约,有关详细信息,请阅读https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM