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);
}
}
}
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);
}
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);
}
}
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);
}
}
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);
}
Aggregations