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