Websockets- 将二进制数据作为块发送 [英] Websockets- send binary data as chunks

查看:42
本文介绍了Websockets- 将二进制数据作为块发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在 websockets 中以块的形式发送二进制数据.?

It is posssible to send binary data as chunks in websockets.?

我使用以下代码发送二进制数据,未来 f = session.getAsyncRemote().sendbinary(bytebuffer);

I am using below code to send binary data, Future f = session.getAsyncRemote().sendbinary(bytebuffer);

这整个传输数据.那么任何人都可以建议我如何将数据作为块异步发送或者它有任何 java 库来实现这个概念.

This transfering data as whole. So can anyone suggest me how to send data as chunks asynchronously or its there any java libary to implement this concept.

推荐答案

WebSocket 类在 nv-websocket-client 有发送碎片帧的方法.以下代码(在 README.md 中找到)显示了如何发送由 3 个分段帧组成的文本消息(你好吗?").您可以用类似的方式发送分片的二进制帧.

WebSocket class in nv-websocket-client has methods to send fragmented frames. The following code (found in README.md) shows how to send a text message ("How are you?") which consists of 3 fragmented frames. You can send fragmented binary frames in a similar way.

// The first frame must be either a text frame or a binary frame.
// And its FIN bit must be cleared.
WebSocketFrame firstFrame = WebSocketFrame
    .createTextFrame("How ")
    .setFin(false);

// Subsequent frames must be continuation frames. The FIN bit of
// all continuation frames except the last one must be cleared.
// Note that the FIN bit of frames returned from
// WebSocketFrame.createContinuationFrame() method is cleared,
// so the example below does not clear the FIN bit explicitly.
WebSocketFrame secondFrame = WebSocketFrame
    .createContinuationFrame("are ");

// The last frame must be a continuation frame with the FIN bit
// set. Note that the FIN bit of frames returned from
// WebSocketFrame.createContinuationFrame methods is cleared,
// so the FIN bit of the last frame must be set explicitly.
WebSocketFrame lastFrame = WebSocketFrame
    .createContinuationFrame("you?")
    .setFin(true);

// Send a text message which consists of 3 frames.
ws.sendFrame(firstFrame)
  .sendFrame(secondFrame)
  .sendFrame(lastFrame);

或者,也可以像上面一样这样做.

Or, alternatively, the same as above can be done like this.

// Send a text message which consists of 3 frames.
ws.sendText("How ", false)
  .sendContinuation("are ")
  .sendContinuation("you?", true);

JavaDoc

http://takahikokawasaki.github.io/nv-websocket-client/

GitHub

https://github.com/TakahikoKawasaki/nv-websocket-client

博客

WebSocket 客户端库(Java SE 1.5+,Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html

WebSocket client library (Java SE 1.5+, Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html

这篇关于Websockets- 将二进制数据作为块发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆