十三、栗子
8. 使用举例
这里使用AIO举例,NIO的例子使用类似于 AIO 的列子
8.1 过滤器类代码举例
package org.voovan.network.messagesplitter;
import org.voovan.network.IoSession;
import org.voovan.network.messagesplitter;
/**
* 按换行对消息分割
*
* @author helyho
*
*/
public class LineMessageSplitter implements MessageSplitter {
@Override
public int canSplite(IoSession session,ByteBuffer byteBuffer) {
byteBuffer.position(byteBuffer.limit()-1);
byte lastByte = byteBuffer.get();
byteBuffer.position(0);
if(byteBuffer.limit() > 1 && lastByte == '\n'){
return byteBuffer.limit();
}
return -1;
}
}
8.2 业务处理句柄代码举例
package org.voovan.test.network;
import java.nio.ByteBuffer;
import org.voovan.network.IoHandler;
import org.voovan.network.IoSession;
import org.voovan.network.MessageLoader;
import org.voovan.tools.log.Logger;
/**
*客户端业务句柄类
**/
public class ClientHandlerTest implements IoHandler {
//连接事件
@Override
public Object onConnect(IoSession session) {
Logger.simple("onConnect"); //在日志平台,输出事件名称
session.setAttribute("key", "attribute value"); //再会话中保存属性
String msg = new String("test message"); //组装一个消息
return msg; //返回消息,在 onConnect 事件发送消息
}
//连接断开事件
@Override
public void onDisconnect(IoSession session) {
Logger.simple("onDisconnect"); //在日志平台,输出事件名称
}
//连接接收事件
@Override
public Object onReceive(IoSession session, Object obj) {
Logger.simple("onRecive: "+obj.toString()); //在日志平台,输出事件名称和接受到的消息内容
Logger.simple("Attribute onRecive: "+session.getAttribute("key")); //输出会话中保存的属性
session.close(); //关闭 Socket 连接
return obj;
}
//异常事件
@Override
public void onException(IoSession session, Exception e) {
Logger.simple("onException"); //在日志平台,输出事件名称和接受到的消息内容
e.printStackTrace(); //输入异常栈信息
}
//消息发送事件
@Override
public void onSent(IoSession session, Object obj) {
ByteBuffer sad = (ByteBuffer)obj; //byte缓冲区
sad = (ByteBuffer)sad.rewind(); //byte缓冲区位置重置
Logger.simple("onSent: "+MessageLoader.byteBufferToString(sad)); //输出事件名称和发送的消息内容
}
}
8.3 服务端类代码举例
package org.voovan.test.network.aio;
import java.io.IOException;
import org.voovan.network.aio.AioServerSocket;
import org.voovan.network.filter.StringFilter;
import org.voovan.test.network.ServerHandlerTest;
public class AioServerSocketTest {
public static void main(String[] args) throws IOException {
AioServerSocket serverSocket = new AioServerSocket("127.0.0.1",2031,1); //构造服务端类实例
serverSocket.handler(new ServerHandlerTest()); //设置业务处理句柄
serverSocket.filterChain().add(new StringFilter()); //设置消息过滤器
serverSocket.start(); //启动服务类
}
}
8.4 客户端类代码举例
package org.voovan.test.network.aio;
import org.voovan.network.aio.AioSocket;
import org.voovan.network.filter.StringFilter;
import org.voovan.test.network.ClientHandlerTest;
import org.voovan.tools.log.Logger;
public class AioSocketTest {
public static void main(String[] args) throws Exception {
AioSocket socket = new AioSocket("127.0.0.1",2031,100); //构造客户端类实例
socket.handler(new ClientHandlerTest()); //设置业务处理句柄
socket.filterChain().add(new StringFilter()); //设置消息过滤器
socket.start(); //启动服务类
}
}
8.5 同步通信代码举例
package org.voovan.test.network.aio;
import org.voovan.network.aio.AioSocket;
import org.voovan.network.filter.StringFilter;
import org.voovan.network.messagesplitter.LineMessageSplitter;
import org.voovan.tools.log.Logger;
public class SyncAioSocketTest {
public static void main(String[] args) throws Exception {
AioSocket socket = new AioSocket("127.0.0.1",2031,30000);
socket.filterChain().add(new StringFilter());
socket.messageSplitter(new LineMessageSplitter());
socket.start();
socket.synchronouSend("syncSocket\r\n");
try {
System.out.println(socket.synchronouRead());
}catch (Exception e){
e.printStackTrace();
}
socket.close();
Logger.simple("Terminate");
}
}