Search in sources :

Example 21 with SocketSession

use of com.generallycloud.baseio.component.SocketSession 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)

Example 22 with SocketSession

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

the class TestWebSocketRumpetrollServlet method doAccept.

@Override
protected void doAccept(HttpSession session, HttpFuture future) throws Exception {
    future.updateWebSocketProtocol();
    session.flush(future);
    msgAdapter.addClient(getAddress(session.getIoSession()), session.getIoSession());
    SocketSession ioSession = session.getIoSession();
    JSONObject o = new JSONObject();
    o.put("type", "welcome");
    o.put("id", ioSession.getSessionId());
    WebSocketFuture f = new WebSocketFutureImpl(ioSession.getContext());
    f.write(o.toJSONString());
    session.flush(f);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture)

Example 23 with SocketSession

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

the class Consumer method push.

// FIXME push 失败时对message进行回收,并移除Consumer
public void push(Message message) throws IOException {
    this.message = message;
    TransactionSection section = attachment.getTransactionSection();
    if (section != null) {
        section.offerMessage(message);
    }
    int msgType = message.getMsgType();
    String content = message.toString();
    SocketSession session = this.session;
    ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), future.getFutureId(), future.getFutureName());
    f.write(content);
    if (msgType == Message.TYPE_TEXT || msgType == Message.TYPE_MAP) {
        session.flush(f);
    } else if (msgType == Message.TYPE_TEXT_BYTE || msgType == Message.TYPE_MAP_BYTE) {
        BytedMessage byteMessage = (BytedMessage) message;
        byte[] bytes = byteMessage.getByteArray();
        f.writeBinary(bytes);
        session.flush(f);
    }
}
Also used : ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) BytedMessage(com.generallycloud.baseio.container.jms.BytedMessage)

Example 24 with SocketSession

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

the class ReconnectableConnector method connect.

public synchronized void connect() {
    if (!reconnect) {
        logger.info("connection is closed, stop to reconnect");
        return;
    }
    SocketSession session = realConnector.getSession();
    ThreadUtil.sleep(300);
    logger.info("begin try to connect");
    for (; ; ) {
        if (session != null && session.isOpened()) {
            logger.error("reconnect failed,try reconnect later on {} milliseconds", retryTime);
            ThreadUtil.sleep(retryTime);
            continue;
        }
        try {
            realConnector.connect();
            break;
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
        logger.error("reconnect failed,try reconnect later on {} milliseconds", retryTime);
        ThreadUtil.sleep(retryTime);
    }
}
Also used : SocketSession(com.generallycloud.baseio.component.SocketSession)

Example 25 with SocketSession

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

the class BalanceReverseAcceptorHandler method accept.

@Override
public void accept(SocketSession session, Future future) throws Exception {
    BalanceFuture f = (BalanceFuture) future;
    if (f.isBroadcast()) {
        balanceFacadeAcceptor.getAcceptor().broadcast(f.translate());
        balanceReverseLogger.logBroadcast(session, future, logger);
        return;
    }
    SocketSession response = balanceRouter.getClientSession(f.getSessionKey());
    if (response == null || response.isClosed()) {
        balanceReverseLogger.logPushLost(session, future, logger);
        return;
    }
    response.flush(f.translate());
    balanceReverseLogger.logPush(session, response, future, logger);
}
Also used : SocketSession(com.generallycloud.baseio.component.SocketSession) BalanceFuture(com.generallycloud.baseio.balance.BalanceFuture)

Aggregations

SocketSession (com.generallycloud.baseio.component.SocketSession)45 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)36 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)35 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)35 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)33 Future (com.generallycloud.baseio.protocol.Future)33 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)29 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)23 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)15 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)13 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)12 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)11 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)9 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)8 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)8 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 JSONObject (com.alibaba.fastjson.JSONObject)4 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)4 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)4