资源简介
springboot 整合了mina 并自定义解码器 复制进去导个包即可食用
代码片段和文件信息
package com.mm.mina;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2018/11/1.
*/
@Component
public class BossMinaHandle extends IoHandlerAdapter {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void messageReceived(IoSession session object message)
throws Exception {
//super.messageReceived(session message);
String rtuStr= (String)message;
System.out.println(rtuStr);
byte[] bt = getCMDBytes(“AA“);
session.write(IoBuffer.wrap(bt));
logger.info(“message :“ + message);
}
private static byte[] getCMDBytes(String cmd) {
String[] cmds = cmd.split(“ “);
byte[] bytes = new byte[cmds.length];
int i=0;
try {
for (String b : cmds) {
if (b.equals(“FF“)) {
bytes[i++] = -1;
} else {
bytes[i++] = (byte) Integer.parseInt(b 16);
//= Byte.parseByte(b 16);
}
}
}catch(Exception e){
e.printStackTrace();
}
return bytes;
}
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}
public static String convertHexToString(String hexs){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
String [] hexArr =hexs.split(“ “);
for(String hex:hexArr){
int decimal = Integer.parseInt(hexs 16);
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}
@Override
public void sessionClosed(IoSession session) throws Exception {
super.sessionClosed(session);
}
@Override
public void sessionIdle(IoSession session IdleStatus status)
throws Exception {
super.sessionIdle(session status);
}
@Override
public void messageSent(IoSession session object message) throws Exception {
logger.info(“发送的消息是:“ + message.toString());
}
@Override
public void sessionCreated(IoSession session) throws Exception {
logger.info(“session is create“+session.getId());
super.sessionCreated(session);
}
@Override
public void sessionOpened(IoSession session) throws Exception {
logger.info(“session is opened“+session.getId())
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-11-06 09:10 springboot 整合mina\
文件 14 2018-11-06 09:10 springboot 整合mina\application.txt
文件 3066 2018-11-06 09:07 springboot 整合mina\BossMinaHandle.java
目录 0 2018-11-06 09:07 springboot 整合mina\codes\
文件 1119 2018-11-01 15:01 springboot 整合mina\codes\MyProtocolCodecFactory.java
文件 3757 2018-11-01 15:34 springboot 整合mina\codes\MyProtocolDecoder.java
文件 1301 2018-11-01 15:10 springboot 整合mina\codes\MyProtocolEncoder.java
文件 2666 2018-11-01 15:02 springboot 整合mina\MinaSocketConfig.java
文件 1397 2018-11-01 15:05 springboot 整合mina\MyKeepAliveMessageFactory.java
文件 742 2018-11-06 09:10 springboot 整合mina\pom.txt
文件 2113 2018-11-01 13:22 springboot 整合mina\SessionMap.java
- 上一篇:管家婆分销ERP_V3_4.1完美破解
- 下一篇:电影数据分析.docx
评论
共有 条评论