本人 JFinal 脑残粉,最近公司几个项目都须要跟硬件交互,这就得用到长链接,以前一直没接触过该领域,原本还想花时间研究下netty,讲真挺难啃的,找资料的时候翻到 t-io,略微了解发现彷佛学习成本极低,没想到做者本人也极其nice,解答我这个门外小伙子好多个问题,顺利用上此框架,恰好解了个人燃眉之急。java
什么是 t-io? t-io是基于java aio实现的即时通信框架,源于做者另外一个久经考验的talent-nio框架,但在易用性、性能及代码可读性方面又远远超越了talent-nio。git
顺便放上传送门:https://my.oschina.net/talenttan/blog/863545api
最新demo代码:https://gitee.com/xiaoxustudent/jfinal-tio服务器
废话不说,下面正题:框架
<dependency> <groupId>com.talent-aio</groupId> <artifactId>talent-aio-server</artifactId> <version>1.6.6.v20170318-RELEASE</version> </dependency>
import java.io.IOException; import com.jfinal.plugin.IPlugin; import com.talent.aio.server.AioServer; import com.talent.aio.server.ServerGroupContext; import com.talent.aio.server.intf.ServerAioHandler; import com.talent.aio.server.intf.ServerAioListener; /** * * @author tanyaowu * @建立时间 2016年11月17日 下午5:59:24 * * @操做列表 编号 | 操做时间 | 操做人员 | 操做说明 (1) | 2016年11月17日 | tanyaowu | 新建类 * */ public class HelloServerStarter implements IPlugin { public static ServerGroupContext<Object, HelloPacket, Object> serverGroupContext = null; static AioServer<Object, HelloPacket, Object> aioServer = null; // 能够为空 static ServerAioHandler<Object, HelloPacket, Object> aioHandler = null; static ServerAioListener<Object, HelloPacket, Object> aioListener = null; static String serverIp = null; static int serverPort = Const.PORT; public static void main(String[] args) throws IOException { aioHandler = new HelloServerAioHandler(); aioListener = null; // 能够为空 serverGroupContext = new ServerGroupContext<>(aioHandler, aioListener); aioServer = new AioServer<>(serverGroupContext); aioServer.start(serverIp, serverPort); } @Override public boolean start() { aioHandler = new HelloServerAioHandler(); aioListener = null; // 能够为空 serverGroupContext = new ServerGroupContext<>(aioHandler, aioListener); aioServer = new AioServer<>(serverGroupContext); try { aioServer.start(serverIp, serverPort); } catch (IOException e) { e.printStackTrace(); return false; } return true; } @Override public boolean stop() { return aioServer.stop(); } }
@Override public void configPlugin(Plugins me) { me.add(new HelloServerStarter()); }
启动结果以下:socket
/** * ************************************************************************** * * @说明: * @项目名称: talent-aio-examples-server * * @author: tanyaowu * @建立时间: 2016年11月18日 上午9:13:15 * * ************************************************************************** */ package nio; import com.talent.aio.common.Aio; import com.talent.aio.common.ChannelContext; import com.talent.aio.server.intf.ServerAioHandler; /** * * @author tanyaowu * @建立时间 2016年11月18日 上午9:13:15 * * @操做列表 编号 | 操做时间 | 操做人员 | 操做说明 (1) | 2016年11月18日 | tanyaowu | 新建类 * */ public class HelloServerAioHandler extends HelloAbsAioHandler implements ServerAioHandler<Object, HelloPacket, Object> { /** * 处理消息 */ @Override public Object handler(HelloPacket packet, ChannelContext<Object, HelloPacket, Object> channelContext) throws Exception { byte[] body = packet.getBody(); if (body != null) { String str = new String(body, HelloPacket.CHARSET); System.out.println("收到消息:" + str); // 绑定长链接 Aio.bindUser(channelContext, "1234"); HelloPacket resppacket = new HelloPacket(); resppacket.setBody(("收到了你的消息,你的消息是:" + str) .getBytes(HelloPacket.CHARSET)); Aio.send(channelContext, resppacket); } return null; } }
而后在中Controller调用Aio.sendToUser(HelloServerStarter.serverGroupContext, getPara(), hello); 发送消息给该客户端。jsp
package controller; import nio.HelloPacket; import nio.HelloServerStarter; import com.jfinal.core.Controller; import com.talent.aio.common.Aio; public class IndexController extends Controller{ public void index(){ render("index.jsp"); } public void aio(){ HelloPacket hello = new HelloPacket(); byte arr[] = {104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100} ; hello.setBody(arr); Aio.sendToUser(HelloServerStarter.serverGroupContext, getPara(), hello); renderJson(); } }
启动项目访问http://localhost/aio/1234,客户端就能收到信息。
ide
2018-11-06 最新截图性能
总结:这是我第一次写博文,有什么写得很差的请多笑纳,也是但愿能帮到有一样需求的人。附上代码:https://gitee.com/xiaoxustudent/jfinal-tio学习