【问题标题】:Restlet client-server streaming using interfaces使用接口的 Restlet 客户端-服务器流式传输
【发布时间】:2013-11-06 08:50:51
【问题描述】:

我想做一些类似于 example posted in restlet's site (first applicaiton) 的事情 - 有一个区别:

我想使用接口流式传输数据 - 不使用原始类型。

我想在客户端和服务器之间定义某种接口,在它们之间流式传输数据并让restlet处理无缝传输数据。

我想到的例子:

interface Streaming {
  InputStream startStream(String streamId);
}

当客户端调用一个调用时,它开始从输入流中读取。服务器接收调用并通过创建输入流(例如,视频文件或只是一些原始数据)开始提供流。 Restlet 应该从服务器端的输入流中读取数据,并在客户端将数据作为输入流提供。

知道如何实现这一点吗? 一个代码示例或一个链接会很棒。谢谢。

【问题讨论】:

  • 查看 ReadableRepresentation。我已经做了类似的事情来发回巨大的 CSV 文件,因为它们是在服务器上构建的。
  • 我不知道这是否可能。我很高兴有一个流式传输的客户端服务器 java 示例(ReadableRepresentation?) - 找不到任何使用 goole :(

标签: java client-server restlet


【解决方案1】:

下面是我到目前为止所学的示例代码 - 一个具有流媒体功能的接口和一个客户端-服务器流媒体示例

我还没有向界面添加参数,它只是下载 - 还没有上传。

界面:

public interface DownloadResource {
    public ReadableRepresentation download();
}

与协议的接口:(逻辑与技术的分离):

public interface DownloadResourceProtocol extends DownloadResource {
    @Get
    @Override
    public ReadableRepresentation download();
}

客户:

ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));

服务器:

public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

配置:

public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}

【讨论】:

    【解决方案2】:

    不确定这是否完全解决了您的问题,但一种方法是创建一个线程,使用 ReadableRepresentation 和管道将数据流回客户端。

    创建管道:

    Pipe pipe = Pipe.open();
    

    创建这样的表示:

    ReadableRepresentation r = new ReadableRepresentation(pipe.source(), mediatype);
    

    启动一个单独的线程,将成批的字节写入管道,如下所示:

    pipe.sink().write(ByteBuffer.wrap(someBytes));
    

    将表示返回给客户端。

    【讨论】:

    • 我正在尝试创建基于流的客户端-服务器接口。我不知道如何使用 ReadableRepresentation 来实现它。界面怎么样?
    • 我现在真的不确定你的意思。您是在尝试实现服务器、客户端还是两者兼而有之?我的答案是关于如何使用 RESTlet 表示从 RESTlet 服务器流式传输数据。
    • 客户端和服务器 - 使用相同的 java 接口 - 使连接无缝。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2014-01-19
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多