Skip to main content

tio-study

tio-study工程简介

一、helloworld

  • helloworld是入门t-io最好的方式!而作者也是用心写了一个对生产项目有参考价值的hello tio,而不是仅仅是show hello而hello

  • 在以前没有文档的日子,t-io第一批用户就是通过这个工程掌握t-io的

  • 服务器端入口程序:org.tio.study.helloworld.server.HelloServerStarter

  • 客户端入口程序:org.tio.study.helloworld.client.HelloClientStarter

  • 本例子演示的是一个典型的TCP长连接应用,大体业务简介如下。

    • 分为server和client工程,server和client共用common工程
      • 服务端和客户端的消息协议比较简单,消息头为4个字节,用以表示消息体的长度,消息体为一个字符串的byte[]
      • 服务端先启动,监听6789端口
      • 客户端连接到服务端后,会主动向服务器发送一条消息
      • 服务器收到消息后会回应一条消息
      • 之后,框架层会自动从客户端发心跳到服务器,服务器也会检测心跳有没有超时(这些事都是框架做的,业务层只需要配一个心跳超时参数即可)
      • 框架层会在断链后自动重连(这些事都是框架做的,业务层只需要配一个重连配置对象即可)
    • 客户端界面
    16:29:38,233 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@2133c8f8 - Registering current configuration as safe fallback point
    2019-06-29 16:29:38 ERROR org.tio.client.TioClient[349]: closeds:0, connections:0
    2019-06-29 16:29:38 INFO org.tio.client.TioClient[310]: [1]: curr:0, closed:0, received:(0p)(0b), handled:0, sent:(0p)(0b)
    2019-06-29 16:29:38 INFO o.t.c.ConnectionCompletionHandler[102]: connected to 127.0.0.1:6789
    收到消息:收到了你的消息,你的消息是:hello world
    2019-06-29 16:29:39 INFO org.tio.client.TioClient[310]: [1]: curr:1, closed:0, received:(1p)(55b), handled:1, sent:(1p)(15b)
    2019-06-29 16:29:40 INFO org.tio.client.TioClient[310]: [1]: curr:1, closed:0, received:(1p)(55b), handled:1, sent:(1p)(15b)
    • 服务器端界面
    2019-06-29 16:29:30,753 INFO  org.tio.server.TioServer[158]: 
    |----------------------------------------------------------------------------------------|
    | Tio on github | https://github.com/tywo45/t-io |
    | Tio site address | https://www.tiocloud.com/ |
    | Tio version | 3.3.3.v20190620-RELEASE |
    | ---------------------------------------------------------------------------------------|
    | TioConfig name | hello-tio-server |
    | Started at | 2019-06-29 16:29:30 |
    | Listen on | 0.0.0.0:6789 |
    | Main Class | org.tio.study.helloworld.server.HelloServerStarter |
    | Jvm start time | 383 ms |
    | Tio start time | 16 ms |
    | Pid | 4632 |
    |----------------------------------------------------------------------------------------|

    收到消息:hello world

二、showcase

  • showcase工程用于进一步掌握t-io,甚至可以用作你项目的脚手架(@精灵007 同学已经用这个工程完成了3个项目)

  • 这里有一篇博客,可以参考:ShowCase设计分析

  • 服务器端入口程序:org.tio.study.showcase.server.ShowcaseServerStarter

  • 客户端入口程序:org.tio.study.showcase.client.ShowcaseClientStarter

  • 客户端界面

    16:24:33,525 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
    16:24:33,526 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5ba23b66 - Registering current configuration as safe fallback point
    使用指南:
    1、需要帮助,输入 '?'.
    2、登录,输入 'login loginname password'.
    3、进入群组,输入 'join group1'.
    4、群聊,输入 'groupMsg group1 text'.
    5、点对点聊天,输入 'p2pMsg loginname text'.
    6、退出程序,输入 'exit'.

    login tan 111
    收到登录响应消息:{"code":1,"time":1561796684113,"token":"1561796684113_1"}
    登录成功,token是:1561796684113_1
  • 服务器端界面

    2019-06-29 16:24:29,011 INFO  org.tio.server.TioServer[158]: 
    |----------------------------------------------------------------------------------------|
    | Tio on github | https://github.com/tywo45/t-io |
    | Tio site address | https://www.tiocloud.com/ |
    | Tio version | 3.3.3.v20190620-RELEASE |
    | ---------------------------------------------------------------------------------------|
    | TioConfig name | null |
    | Started at | 2019-06-29 16:24:29 |
    | Listen on | 0.0.0.0:5678 |
    | Main Class | org.tio.study.showcase.server.ShowcaseServerStarter |
    | Jvm start time | 523 ms |
    | Tio start time | 17 ms |
    | Pid | 21016 |
    |----------------------------------------------------------------------------------------|

    2019-06-29 16:24:33,766 INFO o.t.e.s.s.ShowcaseServerTioListener[44]: onAfterConnected channelContext: server:0.0.0.0:5678, client:127.0.0.1:58326, isConnected:true, isReconnect:false
    2019-06-29 16:24:44,112 INFO o.t.e.s.s.h.LoginReqHandler[61]: 收到登录请求消息:{"loginname":"tan","password":"111","time":1561796683947}
    2019-06-29 16:24:44,125 INFO o.t.e.s.s.ShowcaseServerTioListener[61]: onAfterSent channelContext: server:0.0.0.0:5678, client:127.0.0.1:58326, packet:{"blockSend":false,"body":"eyJjb2RlIjoxLCJ0aW1lIjoxNTYxNzk2Njg0MTEzLCJ0b2tlbiI6IjE1NjE3OTY2ODQxMTNfMSJ9","byteCount":0,"fromCluster":false,"id":2,"sslEncrypted":false,"synSeq":0,"type":2}, isSentSuccess:true

三、IM

  • im项目在1.7.0版本前一直都开放的(见:https://github.com/tywo45/t-io/tree/v1.7.0),考虑到im的复杂性,这会给作者带来一些额外的咨询工作,所以在后面的版本没有放出来,现在重新放出来
  • j-im项目是在本项目的基础上改造而来的,有兴趣的可以看看j-im最早的fork版本
  • 服务器端入口程序:org.tio.study.im.server.starter.ImServerStarter
  • 客户端入口程序:org.tio.study.im.client.starter.ImClientStarter
  • 看看爆裂的性能数据吧 每秒收发540多万条聊天消息