WebSocket集成XMPP网页即时通讯3:二进制文件收发
2016-01-15 14:26
381 查看
WebSocket支持二进制的发送,见jetty官网:
http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-send-message.html
http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-send-message.html
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 endpoint ByteBuffer 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 1 ByteBuffer 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 endpoint ByteBuffer 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 endpoint ByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 }); try { Future<Void> 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<Void> 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 endpoint ByteBuffer buf = ByteBuffer.wrap(new byte[] { 0x11, 0x22, 0x33, 0x44 }); Future<Void> 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); } }
相关文章推荐
- Volley框架的初步理解(一)
- 【第十一章】 SSH集成开发积分商城 之 11.2 实现通用层 ——跟我学spring3
- WCF 双工模式
- Dplasma调度方式——DAGUE_SCHEDULER_LFQ(Local Flat Queues)
- linux 源码安装lnmp+memcache(问题纠正整理)
- Hybris new extension
- jQuery + JavaScript 实现的动态添加文本框功能 和 动态删除文本框功能(二)
- 2416开发记录六: s3c2410_gpio_setpin()系列函数
- 记录为我所用的链
- hive组件二次开发之HiveF
- 安装universal-ctags
- UIKit: UIResponder(转自南峰子博客)
- 批量ssh执行命令
- 数据库删除重复数据
- 批量ssh执行命令
- 职业生涯管理系统 servlet+javabean+mysql(三)登陆的实现
- 菜鸟之路-浅谈设计模式之原型模式
- NSMutableAttributedString,图文混排,字号变换,颜色变换
- 第一章 rsyslog整体架构
- error:LNK2005 已经在*.obj中定义