<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-integration-beans</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.0.13</version>
<type>bundle</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-integration-spring</artifactId>
<version>1.1.7</version>
</dependency>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
</plugin>
package com.he.server;
import java.nio.charset.Charset;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineDecoder;
import org.apache.mina.filter.codec.textline.TextLineEncoder;
public class MyCodeFactory implements ProtocolCodecFactory {
private final TextLineEncoder encoder;
private final TextLineDecoder decoder;
public MyCodeFactory() {
this(Charset.forName("utf-8"));
}
public MyCodeFactory(Charset charset) {
encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);
decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);
}
public ProtocolDecoder getDecoder(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
return decoder;
}
public ProtocolEncoder getEncoder(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
return encoder;
}
public int getEncoderMaxLineLength() {
return encoder.getMaxLineLength();
}
public void setEncoderMaxLineLength(int maxLineLength) {
encoder.setMaxLineLength(maxLineLength);
}
public int getDecoderMaxLineLength() {
return decoder.getMaxLineLength();
}
public void setDecoderMaxLineLength(int maxLineLength) {
decoder.setMaxLineLength(maxLineLength);
}
}
package com.he.server;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
public class MyKeepAliveMessageFactory implements KeepAliveMessageFactory{
private final Logger LOG = Logger.getLogger(MyKeepAliveMessageFactory.class);
/** 心跳包内容 */
private static final String HEARTBEATREQUEST = "1111";
private static final String HEARTBEATRESPONSE = "1112";
public Object getRequest(IoSession session) {
LOG.warn("请求预设信息: " + HEARTBEATREQUEST);
return HEARTBEATREQUEST;
}
public Object getResponse(IoSession session, Object request) {
LOG.warn("响应预设信息: " + HEARTBEATRESPONSE);
/** 返回预设语句 */
return HEARTBEATRESPONSE;
}
public boolean isRequest(IoSession session, Object message) {
LOG.warn("请求心跳包信息: " + message);
if (message.equals(HEARTBEATREQUEST))
return true;
return false;
}
public boolean isResponse(IoSession session, Object message) {
LOG.warn("响应心跳包信息: " + message);
if(message.equals(HEARTBEATRESPONSE))
return true;
return false;
}
}
package com.he.server;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class MyHandler extends IoHandlerAdapter {
//private final int IDLE = 3000;//(单位s)
private final Logger LOG = Logger.getLogger(MyHandler.class);
// public static Set<IoSession> sessions = Collections.synchronizedSet(new HashSet<IoSession>());
public static ConcurrentHashMap<Long, IoSession> sessionsConcurrentHashMap = new ConcurrentHashMap<Long, IoSession>();
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
session.closeOnFlush();
LOG.warn("session occured exception, so close it." + cause.getMessage());
}
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
String str = message.toString();
LOG.warn("客户端" + ((InetSocketAddress) session.getRemoteAddress()).getAddress().getHostAddress() + "连接成功!");
session.setAttribute("type", message);
String remoteAddress = ((InetSocketAddress) session.getRemoteAddress()).getAddress().getHostAddress();
session.setAttribute("ip", remoteAddress);
LOG.warn("服务器收到的消息是:" + str);
session.write("welcome by he");
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
LOG.warn("messageSent:" + message);
}
@Override
public void sessionCreated(IoSession session) throws Exception {
LOG.warn("remote client [" + session.getRemoteAddress().toString() + "] connected.");
// my
Long time = System.currentTimeMillis();
session.setAttribute("id", time);
sessionsConcurrentHashMap.put(time, session);
}
@Override
public void sessionClosed(IoSession session) throws Exception {
LOG.warn("sessionClosed.");
session.closeOnFlush();
// my
sessionsConcurrentHashMap.remove(session.getAttribute("id"));
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
LOG.warn("session idle, so disconnecting......");
session.closeOnFlush();
LOG.warn("disconnected.");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
LOG.warn("sessionOpened.");
//
//session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDLE);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有