スマートソケットは、究極を達成するために、国内のオープンソースのJavaフレームワークAIO、コードの量の追求、パフォーマンス、安定性、およびインタフェースデザインのすべての側面です。スマートソケットためのヒント場合は、私たちのスタープロジェクトを見てください、あなたに追いつく、あなたはスマートソケットに満足できない場合は、彼のようないくつかの忍耐は、スマートソケットが良く取得しようとしてきたこと。
Mavenの
<!-- https://mvnrepository.com/artifact/org.smartboot.socket/aio-core -->
<dependency>
<groupId>org.smartboot.socket</groupId>
<artifactId>aio-core</artifactId>
<version>1.4.8</version>
</dependency>
アップデート:
- 簡略化され、多くの以前のバージョンに比べ、サーバスレッドモデルの再構築、。
- サーバーサポートカスタムバックログ。
- 新しいステート・マシン:ACCEPT_EXCEPTIONサーバーがクライアントの私達accpetイベントを処理するときに、例外がトリガされます。
- 他の小最適化コード。
クイックスタート:
- 定義されたプロトコル
public class StringProtocol implements Protocol<String> { @Override public String decode(ByteBuffer readBuffer, AioSession<String> session) { byte length = readBuffer.get(readBuffer.position()); if (length+1 < readBuffer.remaining()) { return null; } byte[] b = new byte[readBuffer.get()]; readBuffer.get(b); return new String(b); } }
-
サーバを起動します
public class Server { public static void main(String[] args) throws IOException { AioQuickServer<String> server = new AioQuickServer<String>(8080, new StringProtocol(), new MessageProcessor<String>() { public void process(AioSession<String> session, String msg) { System.out.println("接受到客户端消息:" + msg); byte[] response = "Hi Client!".getBytes(); byte[] head = {(byte) response.length}; try { session.writeBuffer().write(head); session.writeBuffer().write(response); } catch (IOException e) { e.printStackTrace(); } } public void stateEvent(AioSession<String> session, StateMachineEnum stateMachineEnum, Throwable throwable) { } }); server.start(); } }
- クライアントを起動します
public class Client { public static void main(String[] args) throws InterruptedException, ExecutionException, IOException { AioQuickClient<String> client = new AioQuickClient<String>("127.0.0.1", 8080, new StringProtocol(), new MessageProcessor<String>() { public void process(AioSession<String> session, String msg) { System.out.println(msg); } public void stateEvent(AioSession<String> session, StateMachineEnum stateMachineEnum, Throwable throwable) { } }); AioSession<String> session = client.start(); byte[] msgBody = "Hello Server!".getBytes(); byte[] msgHead = {(byte) msgBody.length}; try { session.writeBuffer().write(msgHead); session.writeBuffer().write(msgBody); session.writeBuffer().flush(); } catch (IOException e) { e.printStackTrace(); } } }
あまりにも高価な何人かの友人に苦しむ、ネットワーキング学習や通信フレームワークの開発から何かをしたい、あなたはスマートソケットを試してみてください。