Search in sources :

Example 1 with SocketSessionEventListener

use of com.generallycloud.baseio.component.SocketSessionEventListener in project baseio by generallycloud.

the class SimpleTestFIxedLengthServerPush method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            SocketSessionManager sessionManager = session.getContext().getSessionManager();
            Map<Integer, SocketSession> sessions = sessionManager.getManagedSessions();
            String msg = future.getReadText();
            String[] arr = msg.split(" ");
            String cmd = arr[0];
            logger.info("msg received: {}", msg);
            if ("list".equals(cmd)) {
                String keys = sessions.keySet().toString();
                future.write(keys);
            } else if ("id".equals(cmd)) {
                future.write(String.valueOf(session.getSessionId()));
            } else if ("push".equals(cmd)) {
                Integer id = Integer.valueOf(arr[1]);
                SocketSession target = sessions.get(id);
                if (target == null) {
                    future.write("offline id: " + id);
                } else {
                    future.write("from [");
                    future.write(String.valueOf(session.getSessionId()));
                    future.write("] push msg>");
                    future.write(arr[2]);
                    target.flush(future);
                    return;
                }
            } else if ("broadcast".equals(cmd)) {
                future.write("from [");
                future.write(String.valueOf(session.getSessionId()));
                future.write("] broadcast msg>");
                future.write(arr[1]);
                sessionManager.broadcast(future);
                return;
            } else {
                future.write("no cmd: " + cmd);
            }
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.addSessionEventListener(new SocketSessionEventListener() {

        @Override
        public void sessionOpened(SocketSession session) throws Exception {
        }

        @Override
        public void sessionClosed(SocketSession session) {
            SocketSessionManager sessionManager = session.getContext().getSessionManager();
            FixedLengthFuture future = new FixedLengthFutureImpl(session.getContext());
            future.write("client left: " + session.getSessionId());
            try {
                sessionManager.broadcast(future);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    });
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    acceptor.bind();
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSessionManager(com.generallycloud.baseio.component.SocketSessionManager) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSessionEventListener(com.generallycloud.baseio.component.SocketSessionEventListener) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Aggregations

SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)1 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)1 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)1 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)1 SocketSession (com.generallycloud.baseio.component.SocketSession)1 SocketSessionEventListener (com.generallycloud.baseio.component.SocketSessionEventListener)1 SocketSessionManager (com.generallycloud.baseio.component.SocketSessionManager)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 Future (com.generallycloud.baseio.protocol.Future)1 IOException (java.io.IOException)1