smart-socket是一款国产开源的Java AIO框架,追求代码量、性能、稳定性、接口设计各方面都达到极致。如果smart-socket对您有一丝帮助,请Star一下我们的项目并持续关注;如果您对smart-socket并不满意,那请多一些耐心,smart-socket一直在努力变得更好。
更新内容:
1、优化客户端Session的使用体验
此前我们的客户端服务想获得AioSession对象进行数据输出很不方便,需要依赖NEW_SESSION状态机,并在消息处理器中开放getSessioni以供外部使用。
public class IntegerClient { public static void main(String[] args) throws Exception { IntegerClientProcessor processor = new IntegerClientProcessor(); AioQuickClient<Integer> aioQuickClient = new AioQuickClient<Integer>("localhost", 8888, new IntegerProtocol(), processor); aioQuickClient.start(); processor.getSession().write(1); Thread.sleep(1000); aioQuickClient.shutdown(); } } public class IntegerClientProcessor implements MessageProcessor<Integer> { private AioSession<Integer> session; @Override public void process(AioSession<Integer> session, Integer msg) { System.out.println("receive data from server:" + msg); } @Override public void stateEvent(AioSession<Integer> session, StateMachineEnum stateMachineEnum, Throwable throwable) { switch (stateMachineEnum) { case NEW_SESSION: this.session = session; break; default: System.out.println("other state:" + stateMachineEnum); } } public AioSession<Integer> getSession() { return session; } }
新版本中调用AioQuickClient.start后便可获取到AioSession对象,使用更方便,代码更简洁。
public class IntegerClient { public static void main(String[] args) throws Exception { IntegerClientProcessor processor = new IntegerClientProcessor(); AioQuickClient<Integer> aioQuickClient = new AioQuickClient<Integer>("localhost", 8888, new IntegerProtocol(), processor); AioSession<Integer> session = aioQuickClient.start(); session.write(1); Thread.sleep(1000); aioQuickClient.shutdown(); } } public class IntegerClientProcessor implements MessageProcessor<Integer> { @Override public void process(AioSession<Integer> session, Integer msg) { System.out.println("receive data from server:" + msg); } @Override public void stateEvent(AioSession<Integer> session, StateMachineEnum stateMachineEnum, Throwable throwable) { System.out.println(" state:" + stateMachineEnum); } }
项目地址:https://gitee.com/smartboot/smart-socket/tree/v1.3.23/
后续smart-socket 1.3.X的版本都在分支上发布,欢迎大家关注。