當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot使用Mina框架进行服务端与客户端数据通信
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot使用Mina框架进行服务端与客户端数据通信
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
pom.xml引入
<dependency><groupId>org.apache.mina</groupId><artifactId>mina-core</artifactId><version>2.1.3</version> </dependency> <dependency><groupId>org.apache.mina</groupId><artifactId>mina-integration-beans</artifactId><version>2.1.3</version> </dependency>服務端創(chuàng)建采集服務TCP線程
@Configuration public class TCPServ {//數(shù)據(jù)采集開關。配置項中獲取@Value("${monitordata.company.electricity.enableSwitch}")private boolean enableSwitch = false;@Value("${monitordata.company.electricity.tcpPort}")private int port;@AutowiredServerHandler serverHandler; //Mina事件類ServerHandler@Beanpublic IoAcceptor companyElectricityTCPServ() throws Exception {if (!enableSwitch) {return null;}IoAcceptor acceptor = new NioSocketAcceptor();acceptor.getSessionConfig().setReadBufferSize(1024 * 1024);//設置緩沖區(qū)acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60*5); //配置會話信息//其中需要注意的是,在服務端和客戶端的代碼里面,如果要傳遞string信息,codec編碼過濾器中,要這么寫:new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))。否則報錯。//acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));acceptor.setHandler(serverHandler); //自定義處理業(yè)務的代碼:自定義的類try {acceptor.bind(new InetSocketAddress(port));//綁定端口號} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("Socket服務器在端口:" + port + "已經(jīng)啟動");return acceptor;} }Mina事件類ServerHandler
@Component public class ServerHandler extends IoHandlerAdapter {@AutowiredMeterCollectsDataService meterCollectsDataService;@AutowiredElectricCollectorService electricCollectorService;@Overridepublic void sessionCreated(IoSession session) throws Exception {session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60*5);String key = session.getRemoteAddress().toString();System.out.println("設備接入:" + key);}@Overridepublic void messageReceived(IoSession session, Object message) throws Exception {String key = session.getRemoteAddress().toString();IoBuffer ioBuffer = (IoBuffer) message;byte[] data = new byte[ioBuffer.limit()];ioBuffer.get(data);String msg = new String(data);System.out.println("收到數(shù)據(jù):" + msg);//發(fā)送數(shù)據(jù)String sendData = ""; session.write(IoBuffer.wrap(sendData.getBytes("utf-8")));}@Overridepublic void exceptionCaught(IoSession session, Throwable cause) throws Exception {System.out.println("exceptionCaught");session.closeNow();}@Overridepublic void sessionIdle(IoSession session, IdleStatus status) throws Exception {if (status == IdleStatus.BOTH_IDLE) {System.out.println("BOTH空閑");session.closeNow();}}@Overridepublic void sessionClosed(IoSession session) throws Exception {System.out.println("sessionClosed");System.out.println("設備斷開:" + session.getRemoteAddress().toString());} }調(diào)試
使用上傳文件中的網(wǎng)絡調(diào)試工具進行測試
總結(jié)
以上是生活随笔為你收集整理的SpringBoot使用Mina框架进行服务端与客户端数据通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】Python 远程连接服
- 下一篇: 阮一峰的JavaScript 的 thi