WebSocket集成XMPP网页即时通讯3:二进制文件收发
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 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);
}
}
WebSocket集成XMPP网页即时通讯3:二进制文件收发的更多相关文章
- WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探
Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特别频繁的应用尚能相安无事, ...
- Android基于XMPP的即时通讯3-表情发送
这篇博文主要讲表情发送的一些东西. 参考:Android基于XMPP的即时通讯1-基本对话 1.准备好资源文件 采用的是emoji的表情,我打包好了,下载地址:http://files.cnblogs ...
- Android基于XMPP的即时通讯2-文件传输
本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...
- Android基于xmpp的即时通讯应用
xmpp是一个通信协议.因为这是个开放的协议,为了节俭开发成本,很多即时应用都采用了这个协议.Android上最常用的组合asmack +openfire.Asmack是smack的android版, ...
- WebRtc(网页即时通讯技术)知识点总结
前言 WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,简而言之它是一个支持网页浏览器进行实时语音对话或视频对话的技术.并且还支持跨平台:window ...
- [Python]实现XMPP协议即时通讯发送消息功能
#-*- coding: utf-8 -*- __author__ = 'tsbc' import xmpp import time #注意帐号信息,必须加@域名格式 from_user = 'che ...
- 【XMPP】基于XMPP的即时通讯解决方案
什么是XMPP 介绍XMPP之前,先来看看GTalk. GTalk是Google推出的IM(Instant Messaging,即时通讯)软件,类似于QQ和MSN. 从技术角度来说,GTalk与QQ和 ...
- Android基于XMPP的即时通讯1-基本对话
闲暇之余,自己写了个简单的即时通讯,基于OpenFire服务器平台. 整个项目包括两个部分,一个是服务器端,一个是android手机端: 一.关于服务器端没什么好说的,下载安装配置即可 推荐下载带ja ...
- 使用 Django WebSocket Redis 搭建在线即时通讯工具
话不多说先上效果图演示 项目:http://112.74.164.107:9990/ 1.安装组建 redis: yum install redis/apt install redis 2.创建虚拟化 ...
随机推荐
- centos7 hive + 远程mysql 搭建笔记
1.require:java环境,本地可用的hadoop,远程可访问的mysql 2.拷贝hive文件(hive-2.2.1) 3.设置环境变量 export HIVE_HOME=/data/spar ...
- centos7 配置ip
1. 切换到root用户下: su root 2.进入network-scripts目录: cd /etc/sysconfig/network-scripts/ 3.该目录下一般第一个文件是主文件,我 ...
- UVa 10163 Storage Keepers (二分 + DP)
题意:有n个仓库,m个管理员,每个管理员有一个能力值P,每个仓库只能由一个管理员看管,但是每个管理员可以看管k个仓库(但是这个仓库分配到的安全值只有p/k,k=0,1,...),雇用的管理员的工资即为 ...
- Mysql分析优化查询的方式
一:查询语句分析 1.通过create index idx_colunmsName on tableName(columns)为某个表的某些字段创建索引,注意主键和唯一键都会自动创建索引: 如为表st ...
- 怎么让挨着的两input之间没有空隙?
问题:在写选项卡的时候,用input做点击事件的切换时,两个input之间会有空隙,使用margin/padding为0或者为负数依旧如此 → 解决:我脑慢的最后才想到是空格影响的,呵呵呵.
- php,ajax上传文件,多文件上传
HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- MIT molecular Biology 笔记10 翻译
视频 https://www.bilibili.com/video/av7973580/ 教材 Molecular biology of the gene 7th edition J.D. Wat ...
- Page页面生命周期——微信小程序
onLoad:function (options) { //页面初始化 console.log('index Load') }, onShow:function () { // ...
- 20169207《Linux内核原理与分析》第七周作业
这周作业基本分为两个方面,第一方面,阅读学习教材「Linux内核设计与实现 (Linux Kernel Development)」第教材第9,10章.第二方面.学习MOOC「Linux内核分析」第五讲 ...
- POJ3176--Cow Bowling(动态规划)
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range ...