MinaTestServer
来源:互联网 发布:矩阵潜袭基础两人 编辑:程序博客网 时间:2024/06/03 21:04
public class Connected {
public static final int cpu = Runtime.getRuntime().availableProcessors();
public static final int port = 9988;
public static void con(){
ObjectSerializationCodecFactory code = new ObjectSerializationCodecFactory();
code.setDecoderMaxObjectSize(Integer.MAX_VALUE);
code.setEncoderMaxObjectSize(Integer.MAX_VALUE);
NioSocketAcceptor acceptor = new NioSocketAcceptor(cpu);
SocketSessionConfig config = acceptor.getSessionConfig();
config.setReuseAddress(true);
config.setTcpNoDelay(false);
config.setSoLinger(0);
config.setReceiveBufferSize(256 * 1024);
config.setSendBufferSize(1024);
acceptor.getFilterChain().addLast("myFilter", new ProtocolCodecFilter(code));
acceptor.setHandler(new ServiceMessageHandler());
try {
acceptor.bind(new InetSocketAddress(port));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Service服务启动:" + port);
}
}
public class ServiceMessageHandler extends IoHandlerAdapter{
public void messageReceived(IoSession session, Object message) throws Exception {
System.out.println("接到..." + (String)message);
}
public void sessionClosed(IoSession session) throws Exception {
System.out.println("一个人走了..." + session.getRemoteAddress());
}
public void sessionOpened(IoSession session) throws Exception {
System.out.println("一个人連上來了..." + session.getRemoteAddress());
}
}