本人 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學習