Search in sources :

Example 6 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class TestWebSocketRumpetrollServlet method accept.

@Override
public void accept(SocketSession session, Future future) throws Exception {
    if (future instanceof HttpFuture) {
        super.accept(session, future);
        return;
    }
    WebSocketFuture f = (WebSocketFuture) future;
    // CLOSE
    if (f.getType() == 8) {
        msgAdapter.removeClient(session);
        JSONObject o = new JSONObject();
        o.put("type", "closed");
        o.put("id", session.getSessionId());
        msgAdapter.sendMsg(o.toJSONString());
        logger.info("客户端主动关闭连接:{}", session);
    } else {
        String msg = f.getReadText();
        JSONObject o = JSON.parseObject(msg);
        String name = o.getString("name");
        if (StringUtil.isNullOrBlank(name)) {
            name = getAddress(session);
        }
        o.put("name", name);
        o.put("id", session.getSessionId());
        String type = o.getString("type");
        if ("update".equals(type)) {
            o.put("life", "1");
            o.put("authorized", "false");
            o.put("x", Double.valueOf(o.getString("x")));
            o.put("y", Double.valueOf(o.getString("x")));
            o.put("momentum", Double.valueOf(o.getString("momentum")));
            o.put("angle", Double.valueOf(o.getString("angle")));
        } else if ("message".equals(type)) {
        }
        msgAdapter.sendMsg(o.toJSONString());
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Example 7 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class HttpFutureAcceptorService method accept.

@Override
public void accept(SocketSession session, Future future) throws Exception {
    HttpSessionManager manager = context.getHttpSessionManager();
    HttpFuture httpReadFuture = (HttpFuture) future;
    HttpSession httpSession = manager.getHttpSession(context, session, httpReadFuture);
    doAccept(httpSession, httpReadFuture);
}
Also used : HttpSessionManager(com.generallycloud.baseio.container.http11.HttpSessionManager) HttpSession(com.generallycloud.baseio.container.http11.HttpSession) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Example 8 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class TestCookieHeaderServlet method doAccept.

@Override
protected void doAccept(HttpSession session, HttpFuture future) throws Exception {
    System.out.println();
    System.out.println();
    String name = future.getRequestParam("name");
    String value = future.getRequestParam("value");
    if (StringUtil.isNullOrBlank(name)) {
        name = "test8";
    }
    if (StringUtil.isNullOrBlank(value)) {
        value = UUIDGenerator.random();
    }
    String res = "yes server already accept your message :) " + future.getRequestParams();
    Cookie c = new Cookie(name, value);
    c.setComment("comment");
    c.setMaxAge(999999);
    future.addCookie(c);
    future.setResponseHeader(name, value);
    future.write(res);
    session.flush(future);
}
Also used : Cookie(com.generallycloud.baseio.codec.http11.future.Cookie)

Example 9 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class TestHttpLoadServerAio method main.

public static void main(String[] args) throws Exception {
    final AtomicInteger res = new AtomicInteger();
    final AtomicInteger req = new AtomicInteger();
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            HttpFuture f = (HttpFuture) future;
            String res;
            if (f.hasBodyContent()) {
                byte[] array = f.getBodyContent();
                res = "yes server already accept your message :) </BR><PRE style='font-size: 18px;color: #FF9800;'>" + new String(array) + "</PRE>";
            } else {
                res = "yes server already accept your message :) " + f.getRequestParams();
            }
            f.write(res);
            session.flush(f);
        // System.out.println("req======================"+req.getAndIncrement());
        }
    };
    ServerConfiguration c = new ServerConfiguration(80);
    c.setSERVER_MEMORY_POOL_CAPACITY(2560000);
    c.setSERVER_MEMORY_POOL_UNIT(256);
    c.setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    c.setSERVER_CORE_SIZE(4);
    c.setSERVER_ENABLE_MEMORY_POOL(true);
    c.setSERVER_MEMORY_POOL_CAPACITY_RATE(0.5);
    SocketChannelContext context = new AioSocketChannelContext(c);
    context.setProtocolFactory(new ServerHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    new SocketChannelAcceptor(context).bind();
    ThreadUtil.sleep(99999999);
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) ServerHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ServerHTTPProtocolFactory) AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 10 with HttpFuture

use of com.generallycloud.baseio.codec.http11.future.HttpFuture in project baseio by generallycloud.

the class TestWebSocketChatServlet method accept.

@Override
public void accept(SocketSession session, Future future) throws Exception {
    if (future instanceof HttpFuture) {
        super.accept(session, future);
        return;
    }
    WebSocketFuture f = (WebSocketFuture) future;
    // CLOSE
    if (f.isCloseFrame()) {
        if (!msgAdapter.removeClient(session)) {
            return;
        }
        JSONObject obj = new JSONObject();
        obj.put("username", session.getAttribute("username"));
        obj.put("numUsers", msgAdapter.getClientSize());
        obj.put("action", "user-left");
        String msg1 = obj.toJSONString();
        msgAdapter.sendMsg(msg1);
    } else {
        String msg = f.getReadText();
        JSONObject obj = JSON.parseObject(msg);
        String action = obj.getString("action");
        if ("new-message".equals(action)) {
            String owner = (String) session.getAttribute("username");
            String message = obj.getString("message");
            if (message.charAt(0) == '@') {
                int nIndex = message.indexOf(' ');
                if (nIndex > 1) {
                    String username = message.substring(1, nIndex);
                    SocketSession s = msgAdapter.getSession(username);
                    if (s == null) {
                        obj.put("message", "用户不存在或者已离线");
                        obj.put("username", owner);
                        msgAdapter.sendMsg(session, obj.toJSONString());
                        return;
                    }
                    obj.put("username", owner);
                    msgAdapter.sendMsg(session, obj.toJSONString());
                    obj.put("username", owner + "@你");
                    obj.put("message", message.substring(nIndex));
                    msgAdapter.sendMsg(s, obj.toJSONString());
                    return;
                }
            }
            obj.put("username", owner);
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else if ("add-user".equals(action)) {
            String username = (String) session.getAttribute("username");
            if (username != null) {
                return;
            }
            username = obj.getString("username");
            if (StringUtil.isNullOrBlank(username)) {
                return;
            }
            msgAdapter.addClient(username, session);
            session.setAttribute("username", username);
            obj.put("numUsers", msgAdapter.getClientSize());
            obj.put("action", "login");
            msgAdapter.sendMsg(session, obj.toJSONString());
            obj.put("username", username);
            obj.put("action", "user-joined");
            msgAdapter.sendMsg(obj.toJSONString());
            obj.put("action", "new-message");
            obj.put("username", "系统消息");
            obj.put("message", "欢迎加入QQ群讨论java io相关技术:540637859,@某人可以单独向他发送消息。");
            msgAdapter.sendMsg(session, obj.toJSONString());
        } else if ("typing".equals(action)) {
            obj.put("username", session.getAttribute("username"));
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else if ("stop-typing".equals(action)) {
            obj.put("username", session.getAttribute("username"));
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else if ("disconnect".equals(action)) {
            msgAdapter.removeClient(session);
            obj.put("username", session.getAttribute("username"));
            obj.put("numUsers", msgAdapter.getClientSize());
            obj.put("action", "user-left");
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else {
            f.write("no action matched:" + action);
            session.flush(f);
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Aggregations

HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)11 SocketSession (com.generallycloud.baseio.component.SocketSession)6 ClientHttpFuture (com.generallycloud.baseio.codec.http11.future.ClientHttpFuture)4 WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)4 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)4 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)4 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)4 JSONObject (com.alibaba.fastjson.JSONObject)3 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)3 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)3 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)3 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)3 HttpClient (com.generallycloud.baseio.codec.http11.HttpClient)2 HttpIOEventHandle (com.generallycloud.baseio.codec.http11.HttpIOEventHandle)2 WebSocketFutureImpl (com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl)2 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)2 Waiter (com.generallycloud.baseio.concurrent.Waiter)2 Future (com.generallycloud.baseio.protocol.Future)2 TimeoutException (com.generallycloud.baseio.TimeoutException)1 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)1