Skip to main content

JFinal 极速集成tio

1.maven拉tio包

<dependency>
<groupId>org.t-io</groupId>
<artifactId>tio-core</artifactId>
<version>3.2.1.v20181024-RELEASE</version>
</dependency>

2.从tio-study拉来helloworld代码

3.HelloClientStarter HelloServerStarter 实现IPlugin,把start实现

public class HelloServerStarter implements IPlugin {
//handler, 包括编码、解码、消息处理
public static ServerAioHandler aioHandler = new HelloServerAioHandler();

//事件监听器,可以为null,但建议自己实现该接口,可以参考showcase了解些接口
public static ServerAioListener aioListener = null;

//一组连接共用的上下文对象
public static ServerTioConfig serverTioConfig = new ServerTioConfig("hello-tio-server", aioHandler, aioListener);

//tioServer对象
public static TioServer tioServer = new TioServer(serverTioConfig);

//有时候需要绑定ip,不需要则null
public static String serverIp = null;

//监听的端口
public static int serverPort = Const.PORT;

/**
* 启动程序入口
*/
public static void main(String[] args) throws IOException {
serverTioConfig.setHeartbeatTimeout(Const.TIMEOUT);
tioServer.start(serverIp, serverPort);
}

@Override
public boolean start() {
serverTioConfig.setHeartbeatTimeout(Const.TIMEOUT);
try {
tioServer.start(serverIp, serverPort);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}

@Override
public boolean stop() {
return false;
}
}

4.启动服务端、客户端搞定

@Override
public void configPlugin(Plugins me) {
me.add(new HelloServerStarter());
me.add(new HelloClientStarter());
}

5.代码

jfinal整合tio