教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 建筑文档 >

xsocket使用指南官方文档翻译(2)

来源:网络收集 时间:2026-08-03
导读: 服务端的默认超时行为可以通过服务器端的超时设置函数修改。 // the handler class Handler implements IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler { publicbooleanonConnectionTimeout(INonBl

服务端的默认超时行为可以通过服务器端的超时设置函数修改。 // 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字,全部文档内容请下载后查看。喜欢就下载吧 ……

xsocket使用指南官方文档翻译(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/681746.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)