WebSocket集成XMPP网页即时通讯3:二进制文件收发
来源:互联网 发布:人工智能在智能交通 编辑:程序博客网 时间:2024/06/08 13:58
WebSocket支持二进制的发送,见jetty官网:
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 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<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 endpointByteBuffer 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); }}
0 0
- WebSocket集成XMPP网页即时通讯3:二进制文件收发
- WebSocket集成XMPP网页即时通讯2:Openfire网关集成
- WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探
- 基于WebSocket的网页端即时通讯技术
- JavaWeb网页聊天室(WebSocket即时通讯)
- 基于WebSocket的网页端即时通讯
- xmpp即时通讯
- xmpp 即时通讯
- XMPP 即时通讯
- XMPP即时通讯
- 即时通讯Xmpp
- 集成websocket即时通讯 java聊天源码 代码下载 java后台框架源码 websocket源码
- 集成websocket即时通讯 java聊天源码 代码下载 java后台框架源码 websocket源码
- 【企业级框架源码】【websocket即时通讯】【集成微信接口开发】【集成代码生成器】
- 【企业级框架源码】【websocket即时通讯】【集成微信接口开发】【集成代码生成器】
- 即时通讯协议-XMPP
- android 即时通讯 XMPP
- xmpp即时通讯详解
- WebRTC开发基础(WebRTC入门系列2:RTCPeerConnection)
- Android 设备屏幕尺寸的确定
- WebRTC开发基础(WebRTC入门系列3:RTCDataChannel)
- 自定义注解
- C#使用七牛云存储上传下载文件、自定义回调
- WebSocket集成XMPP网页即时通讯3:二进制文件收发
- WebSocket集成XMPP网页即时通讯2:Openfire网关集成
- WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探
- 优化openfire服务器提升xmpp 效率的15个方法(原创)
- 机器学习研究会每周精华0917
- Liferay开发实战(1):入门
- Liferay开发实战(2):Service Builder生成持久化层,及开发服务层
- 配置nginx的端口来访问不同网站
- Java异步执行多个HTTP请求的例子(需要apache http类库)