Socket.D v2.5.11

Stream 的三个应用接口

</> markdown

在 Session 的接口中,会看到 SendStream、RequestStream 和 SubscribeStream。它们有什么区别?

1、Stream 的基础接口

public interface Stream<T extends Stream> {
    //流Id
    String sid();

    //是否完成
    boolean isDone();

    //异常发生时
    T thenError(Consumer<Throwable> onError);

    //进度发生时(发送或接收的进度) @param onProgress (isSend, val, max)
    T thenProgress(TriConsumer<Boolean, Integer, Integer> onProgress);
}

2、Stream 的三个应用接口

  • SendStream 发送流
public interface SendStream extends Stream<SendStream>{}
  • RequestStream 请求流
public interface RequestStream extends Stream<RequestStream> {
    //异步等待获取答复
    Reply await();

    //答复发生时
    RequestStream thenReply(IoConsumer<Reply> onReply);
}
  • SubscribeStream 订阅流
public interface SubscribeStream extends Stream<SubscribeStream> {
    //答复发生时
    SubscribeStream thenReply(IoConsumer<Reply> onReply);
}