小徐同学

JFinal 极速集成tio

发表于:2018-12-26 7:12:02 点击量:0 赞:未知

1.maven拉tio包


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

2.从tio-showcase拉来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 ServerGroupContext serverGroupContext = new ServerGroupContext("hello-tio-server", aioHandler, aioListener);

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

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

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

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

    @Override
    public boolean start() {
        serverGroupContext.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

TCP连接数:, IP数:
    发 送