NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast( "codec", new HttpServerCodec());
acceptor.setHandler(new IoHandlerAdapter() {
public void messageReceived(IoSession session, Object message)throws Exception {
if (message instanceof HttpRequestImpl) {
HttpRequestImpl request1 = (HttpRequestImpl) message;
}
if (message instanceof IoBuffer) {
SocketAddress addr = session.getRemoteAddress()
IoBuffer simbuf = (IoBuffer) message;
byte[] buff = simbuf.array();
String decoded = new String(buff, "UTF-8");
System.out.println(decoded);
}
if (message instanceof HttpEndOfContent) {
byte[] bytes = "OK".getBytes();
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Length", Integer.toString(bytes.length));
DefaultHttpResponse resp = new DefaultHttpResponse(
HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers);
session.write(resp);
session.write(IoBuffer.wrap(bytes));
session.write(new HttpEndOfContent());
}
}
});
try {
acceptor.bind( new InetSocketAddress(1234) );
} catch (IOException e) {
e.printStackTrace();
}