博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebSocket集成XMPP网页即时通讯3:二进制文件收发
阅读量:4135 次
发布时间:2019-05-25

本文共 2368 字,大约阅读时间需要 7 分钟。

WebSocket支持二进制的发送,见jetty官网:

 

Blocking Send Message 阻塞式

Most calls are blocking in nature, and will not return until the send has completed (or has thrown an exception).

RemoteEndpoint remote = session.getRemote(); // Blocking Send of a BINARY message to remote endpointByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });try{    remote.sendBytes(buf);}catch (IOException e){    e.printStackTrace(System.err);}

 

Send Partial Message 分块式

RemoteEndpoint remote = session.getRemote(); // Blocking Send of a BINARY message to remote endpoint// Part 1ByteBuffer buf1 = ByteBuffer.wrap(new byte[] { 0x11, 0x22 });// Part 2 (last part)ByteBuffer buf2 = ByteBuffer.wrap(new byte[] { 0x33, 0x44 });try{    remote.sendPartialBytes(buf1,false);    remote.sendPartialBytes(buf2,true); // isLast is true}catch (IOException e){    e.printStackTrace(System.err);}

 

Async Send Message 异步

 

//Example 29.7. Send Binary Message (Async Simple)RemoteEndpoint remote = session.getRemote(); // Async Send of a BINARY message to remote endpointByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });remote.sendBytesByFuture(buf);
// Example 29.8. Send Binary Message (Async, Wait Till Success)RemoteEndpoint remote = session.getRemote(); // Async Send of a BINARY message to remote endpointByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });try{    Future
fut = remote.sendBytesByFuture(buf); // wait for completion (forever) fut.get();}catch (ExecutionException | InterruptedException e){ // Send failed e.printStackTrace();}How to send a simple Binary message using the RemoteEndpoint, tracking the Future
to know if the send succeeded or failed.
//Example 29.9. Send Binary Message (Async, timeout of send)RemoteEndpoint remote = session.getRemote(); // Async Send of a BINARY message to remote endpointByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 });Future
fut = null;try{ fut = remote.sendBytesByFuture(buf); // wait for completion (timeout) fut.get(2,TimeUnit.SECONDS);}catch (ExecutionException | InterruptedException e){ // Send failed e.printStackTrace();}catch (TimeoutException e){ // timeout e.printStackTrace(); if (fut != null) { // cancel the message fut.cancel(true); }}

 

 

转载地址:http://mxpvi.baihongyu.com/

你可能感兴趣的文章
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
Linux查看mac地址
查看>>
Linux修改ip
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
IPS开发手记【一】
查看>>
Java通用字符处理类
查看>>
文件上传时生成“日期+随机数”式文件名前缀的Java代码
查看>>
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>