1 20 package org.apache.mina.example.httpserver.codec; 21 22 import org.apache.mina.common.IdleStatus; 23 import org.apache.mina.common.IoHandler; 24 import org.apache.mina.common.IoHandlerAdapter; 25 import org.apache.mina.common.IoSession; 26 import org.apache.mina.util.SessionLog; 27 28 34 public class ServerHandler extends IoHandlerAdapter { 35 @Override 36 public void sessionOpened(IoSession session) { 37 session.setIdleTime(IdleStatus.BOTH_IDLE, 60); 39 } 40 41 @Override 42 public void messageReceived(IoSession session, Object message) { 43 HttpResponseMessage response = new HttpResponseMessage(); 45 response.setContentType("text/plain"); 46 response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS); 47 response.appendBody("CONNECTED"); 48 49 58 65 if (response != null) { 66 session.write(response).join(); 67 } 68 } 69 70 @Override 71 public void sessionIdle(IoSession session, IdleStatus status) { 72 SessionLog.info(session, "Disconnecting the idle."); 73 session.close(); 74 } 75 76 @Override 77 public void exceptionCaught(IoSession session, Throwable cause) { 78 session.close(); 79 } 80 } 81 | Popular Tags |