1. 程式人生 > >smart-socket v1.3.23 釋出:優化升級通訊框架

smart-socket v1.3.23 釋出:優化升級通訊框架

  

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的版本都在分支上釋出,歡迎大家關注。