xsocket使用指南官方文档翻译(2)
服务端的默认超时行为可以通过服务器端的超时设置函数修改。 // the handler
class Handler implements IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler {
publicbooleanonConnectionTimeout(INonBlockingConnectionnbc) throws IOException {
nbc.write(\nbc.close();
return true; // prevent, that xSocket also closes the connection }
publicbooleanonIdleTimeout(INonBlockingConnectionnbc) throws IOException {
nbc.write(\
nbc.setIdleTimeoutMillis(30 * 1000); // resets the timeout counter return true; // prevent, that xSocket closes the connection }
publicbooleanonData(INonBlockingConnectionnbc) throws IOException { //...
return true; } }
// and the server
IServer server = new Server(8090, new Handler());
server.setIdleTimeoutMillis(30 * 1000); // set the default idle timeout for server-side connections
server.run();
11、回调方法的同步
onConnect和onData这样的回调方法会根据连接同步处理,也就是说对伊同一个连接,如果onConnect不执行完成,那么用户也不会调用onData。
class Handler implements IConnectHandler, IDataHandler {
publicbooleanonConnect(INonBlockingConnectionnbc) throws IOException { nbc.write(\ //...
// DO NOT DO THIS!
// this causes that onData() will never be called because executing of // callback methods is synchronized based on the connection while (true) { try {
Thread.slepp(1000);
} catch (InterruptedException ignore) { } nbc.write(\ }
// You could define a TimerTask and run it within a Timer (thread) // to implement the \return true; }
publicbooleanonData(INonBlockingConnectionnbc) throws IOException { String msg = nbc.readStringByDelimiter(\ //... return true; } }
12、定义连接作用域的Handler
默认的Handler作用域是实例作用域(全局)。也就是同一个Handler可以用于每一个新进入的连接。一个Handler通过实现IConnectionScoped接口来转变成连接作用域的。这个接口需要一个可以被用来为新连接创建专门Handler实例的clone方法。为了避免副作用,clone方法需要实现成深度拷贝。
classSmtpHandler implements IDataHandler, IConnectionScoped {
privateinthandledMessages = 0;
privateSessionDatasessionData = new SessionData();
publicbooleanonData(INonBlockingConnectionnbc) throws IOException { //... return true; }
// deep clone: all attributes beside primitives, immutable or // global manager/service references have also to be cloned public Object clone() throws CloneNotSupportedException { SmtpHandler copy = (SmtpHandler) super.clone();
copy.sessionData = (SessionData) this.sessionData.clone(); return copy; } }
通过声明Handler为连接作用域,在支持深度拷贝的情况下,Handler的变量自动变成连接指定的。同时除了这种显式的往连接发送数据的方法之外,隐式的方法也是支持的。
13、绑定会话相关的数据到连接
一个可选的向连接分发会话数据的方法是直接附加数据。一般而言,这是推荐的方法。一个连接支持附件特定连接的会话数据通过setAttachment(object),getAttachment()方法。
classSmtpHandler implements IConnectHandler, IDataHandler {
publicbooleanonConnect(INonBlockingConnectionnbc) throws IOException { nbc.setAttachment(new SessionData()); return true; }
publicbooleanonData(INonBlockingConnectionnbc) throws IOException { SessionDatasessionData = (SessionData) nbc.getAttachment(); //... return true; } }
14、运行时替换Handler
xSocket支持在运行时替换Handler,这可以在服务器端为所有新进入的连接应用新的Handler。如果连接已经建立,可以调用setHandler方法来替换当前连接的Handler。
classServerHandlerA implements IDataHandler {
publicbooleanonData(INonBlockingConnectionnbc) throws IOException { String cmd = nbc.readStringByDelimiter(\if (cmd.equals(\
nbc.setHandler(new ServerHandlerB()); } else {
nbc.write(\ }
return true; } }
在服务器端Handler赋给了server,这个Handler会赋值给每一个新进入的连接,可以通过调用setHandler方法来替换。
classServerSideHandler implements IDataHandler { @Resource
private Server server;
publicbooleanonData(INonBlockingConnection connection) throws IOException {
String cmd = connection.readStringByDelimiter(\if (cmd.equals(\
server.setHandler(new ServerHandlerB()); connection.write(\connection.write(\
} else {
connection.write(\ }
return true; } }
15、示例:简单的基于长度的Handler 为了应用基于长度的通信方法,连接的mark-support可以使用,这种情况下,客户端首先写一个空的长度属性,在写完内容数据之后,写指针会移回到长度属性域来覆盖长度属性域。
IBlockingConnectionbc = new BlockingConnection(host, port);
bc.setAutoflush(false); // mark support requires deactivated autoflush!
bc.markWritePosition(); // mark current position bc.write((int) 0); // write \
int written = bc.write(\
written += bc.write(\//...
bc.resetToWriteMark(); // return to length field position bc.write(written); // and update it
bc.flush(); // flush (marker will be removed implicit) 16、服务器端
publicbooleanonData(INonBlockingConnectionnbc) throwsIOException,
BufferUnderflowException {
// validate that enough data is available (if
// not an BufferUnderflowException will be thrown)
int length = ConnectionUtils.validateSufficientDatasizeByIntLengthField(nbc) String text = nbc.readStringByLength(length);
nbc.write(text); return true; }
17、更复杂的例子:多部分数据 一般而言,一个数据记录包含有多个部分。比如一个数据记录以头字段开始,然后接着是数据内容,典型的头包含有一个长度字段。
IBlockingConnectionbc = new BlockingConnection(host, port); bc.setAutoflush(false);
bc.write(RECORD_TYPE_A); // record type bc.write((int) 2); // version bc.write(sign); …… 此处隐藏:6070字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [建筑文档]2018年公需课:专业技术人员创新能力与
- [建筑文档]2013年福建教师招考小学数学历年真题
- [建筑文档]高中信息技术课flash知识点总结 - 图文
- [建筑文档]电工实训 - 图文
- [建筑文档]最高院公告案例分析100篇(民商篇)
- [建筑文档]南开中学高2017级14-15学年(上)期末
- [建筑文档]五粮液集团战略分析
- [建筑文档]鲁教版(2012秋季版)九年级化学 酸碱
- [建筑文档]超星尔雅2017中国哲学概论自整理题库答
- [建筑文档]关于成为海口金盘饮料公司材料独家供货
- [建筑文档]LNG学习资料第一册 基础知识 - 图文
- [建筑文档]四年级品社下册《好大一个家》复习资料
- [建筑文档]现阶段领导权力腐败的特点及发展趋势
- [建筑文档]魏晋南北朝诗歌鉴赏—嵇康
- [建筑文档]坚持追求真爱是理智的行为 正方一辩稿
- [建筑文档]湘西州刑释解教人员帮教安置工作存在的
- [建筑文档]园林工程试题库及答案
- [建筑文档]计算机长期没有向WSUS报告状态
- [建筑文档]日语最新流行语
- [建筑文档]B62-016 景观进场交底专题会议
- 2018年中考语文课内外古诗词鉴赏专题复
- 高考试题研究心得体会
- C语言基础题及答案
- 电气控制及PLC习题及答案
- 都昌小学家长学校汇报材料
- GMAT作文模板正确使用方法
- 俄军办坦克大赛:中国99式有望与豹2A6
- 成本会计练习题
- 酒店餐饮业最流行的5S管理方法
- 2014-2015学年山东省菏泽市高二(下)
- 《黄鹤楼送孟浩然之广陵》教案、说课、
- 2013年结构化学自测题 有答案版
- 2011西安世界园艺博览会游览解说词(附
- 窗口文明单位示范单位创建活动总结
- 2018满分超星尔雅就业课后练习期末答案
- 韶山市城市总体规划-基础资料
- 苏教版第三单元知识点归纳
- 第4章 曲轴模态分析
- 加大查办案件力度的思考
- 武汉CPC导轨介绍




