簡體   English   中英

使用RxScala進行反應式編程

[英]Reactive Programming using RxScala

我有一個Observable通過Socket協議連接到服務。 與套接字的連接通過客戶端庫進行。 我使用的客戶端庫有java.util.Observer,我可以注冊被推入的事件

final class MyObservable extends Observable[MyEvent] {

  def subscribe(subscriber: Subscriber[MyEvent]) = {
    // connect to the Socket (Step: 1)
    // get the responses that are pushed (Step: 2)
    // transform them into MyEvent type (Step: 3)
  }
}

我有兩個我不明白的開放性問題。

如何在訂閱服務器中獲得Step:3的結果?

每次當我得到MyEvent時,如下面的訂閱者,我看到有一個新的連接被創建。 最后,為每個傳入事件運行步驟1,步驟2和步驟3。

val myObservable = new MyObservale()
myObservable.subscribe()

除非我誤解你的問題,否則你只需要onNext

def subscribe(subscriber: Subscriber[MyEvent]) = {
  // connect to the Socket (Step: 1)
  // get the responses that are pushed (Step: 2)
  // transform them into MyEvent type (Step: 3)

  // finally notify the subscriber:
  subscriber.onNext(myEventFromStep3)
}

和訂閱的代碼將執行以下操作:

myObservable.subscribe(onNext = println(_))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM