十三、栗子

8. 使用举例

这里使用AIO举例,NIO的例子使用类似于 AIO 的列子
8.1 过滤器类代码举例
  1. package org.voovan.network.messagesplitter;
  2. import org.voovan.network.IoSession;
  3. import org.voovan.network.messagesplitter;
  4. /**
  5. * 按换行对消息分割
  6. *
  7. * @author helyho
  8. *
  9. */
  10. public class LineMessageSplitter implements MessageSplitter {
  11. @Override
  12. public int canSplite(IoSession session,ByteBuffer byteBuffer) {
  13. byteBuffer.position(byteBuffer.limit()-1);
  14. byte lastByte = byteBuffer.get();
  15. byteBuffer.position(0);
  16. if(byteBuffer.limit() > 1 && lastByte == '\n'){
  17. return byteBuffer.limit();
  18. }
  19. return -1;
  20. }
  21. }

8.2 业务处理句柄代码举例
  1. package org.voovan.test.network;
  2. import java.nio.ByteBuffer;
  3. import org.voovan.network.IoHandler;
  4. import org.voovan.network.IoSession;
  5. import org.voovan.network.MessageLoader;
  6. import org.voovan.tools.log.Logger;
  7. /**
  8. *客户端业务句柄类
  9. **/
  10. public class ClientHandlerTest implements IoHandler {
  11. //连接事件
  12. @Override
  13. public Object onConnect(IoSession session) {
  14. Logger.simple("onConnect"); //在日志平台,输出事件名称
  15. session.setAttribute("key", "attribute value"); //再会话中保存属性
  16. String msg = new String("test message"); //组装一个消息
  17. return msg; //返回消息,在 onConnect 事件发送消息
  18. }
  19. //连接断开事件
  20. @Override
  21. public void onDisconnect(IoSession session) {
  22. Logger.simple("onDisconnect"); //在日志平台,输出事件名称
  23. }
  24. //连接接收事件
  25. @Override
  26. public Object onReceive(IoSession session, Object obj) {
  27. Logger.simple("onRecive: "+obj.toString()); //在日志平台,输出事件名称和接受到的消息内容
  28. Logger.simple("Attribute onRecive: "+session.getAttribute("key")); //输出会话中保存的属性
  29. session.close(); //关闭 Socket 连接
  30. return obj;
  31. }
  32. //异常事件
  33. @Override
  34. public void onException(IoSession session, Exception e) {
  35. Logger.simple("onException"); //在日志平台,输出事件名称和接受到的消息内容
  36. e.printStackTrace(); //输入异常栈信息
  37. }
  38. //消息发送事件
  39. @Override
  40. public void onSent(IoSession session, Object obj) {
  41. ByteBuffer sad = (ByteBuffer)obj; //byte缓冲区
  42. sad = (ByteBuffer)sad.rewind(); //byte缓冲区位置重置
  43. Logger.simple("onSent: "+MessageLoader.byteBufferToString(sad)); //输出事件名称和发送的消息内容
  44. }
  45. }

8.3 服务端类代码举例
  1. package org.voovan.test.network.aio;
  2. import java.io.IOException;
  3. import org.voovan.network.aio.AioServerSocket;
  4. import org.voovan.network.filter.StringFilter;
  5. import org.voovan.test.network.ServerHandlerTest;
  6. public class AioServerSocketTest {
  7. public static void main(String[] args) throws IOException {
  8. AioServerSocket serverSocket = new AioServerSocket("127.0.0.1",2031,1); //构造服务端类实例
  9. serverSocket.handler(new ServerHandlerTest()); //设置业务处理句柄
  10. serverSocket.filterChain().add(new StringFilter()); //设置消息过滤器
  11. serverSocket.start(); //启动服务类
  12. }
  13. }

8.4 客户端类代码举例
  1. package org.voovan.test.network.aio;
  2. import org.voovan.network.aio.AioSocket;
  3. import org.voovan.network.filter.StringFilter;
  4. import org.voovan.test.network.ClientHandlerTest;
  5. import org.voovan.tools.log.Logger;
  6. public class AioSocketTest {
  7. public static void main(String[] args) throws Exception {
  8. AioSocket socket = new AioSocket("127.0.0.1",2031,100); //构造客户端类实例
  9. socket.handler(new ClientHandlerTest()); //设置业务处理句柄
  10. socket.filterChain().add(new StringFilter()); //设置消息过滤器
  11. socket.start(); //启动服务类
  12. }
  13. }
8.5 同步通信代码举例
  1. package org.voovan.test.network.aio;
  2. import org.voovan.network.aio.AioSocket;
  3. import org.voovan.network.filter.StringFilter;
  4. import org.voovan.network.messagesplitter.LineMessageSplitter;
  5. import org.voovan.tools.log.Logger;
  6. public class SyncAioSocketTest {
  7. public static void main(String[] args) throws Exception {
  8. AioSocket socket = new AioSocket("127.0.0.1",2031,30000);
  9. socket.filterChain().add(new StringFilter());
  10. socket.messageSplitter(new LineMessageSplitter());
  11. socket.start();
  12. socket.synchronouSend("syncSocket\r\n");
  13. try {
  14. System.out.println(socket.synchronouRead());
  15. }catch (Exception e){
  16. e.printStackTrace();
  17. }
  18. socket.close();
  19. Logger.simple("Terminate");
  20. }
  21. }