Websockets-将二进制数据作为块发送

时间:2015-04-14 13:57:51

标签: java websocket

在websockets中将二进制数据作为块发送是可能的。

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

这整个传输数据。所以任何人都可以建议我如何以异步方式发送数据或者使用任何java库来实现这个概念。

1 个答案:

答案 0 :(得分:0)

nv-websocket-client中的

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

// 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);

或者,也可以像这样完成上述相同的工作。

// 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