// NetServerTest1 package ProtobufTest; import com.apanda.net.impl.NetServer; public class NetServerTest1 { public static void main(String[] args) { ServerListenerTest1 serverListenerTest = new ServerListenerTest1(); NetServer netserver = new NetServer((short)8787, NetServer.ConnectType.SOCKET_SERVER, serverListenerTest); netserver.start(); } }
// NetClientTest1
package ProtobufTest; import com.apanda.net.impl.NetClient; public class NetClientTest1 { public static void main(String[] args) { ClientListenerTest1 clientListenerTest = new ClientListenerTest1(); NetClient netClient = new NetClient("192.168.1.105", (short) 8787, clientListenerTest); netClient.connect(); } }
//ServerListenerTest1
package ProtobufTest; import com.apanda.db.util.Xxtea; import com.apanda.message.login.CSLoginMsg; import com.apanda.net.impl.ServerListener; import com.apanda.net.util.NetFunction; import com.google.protobuf.InvalidProtocolBufferException; import io.netty.channel.ChannelHandlerContext; import java.io.IOException; import java.util.Arrays; public class ServerListenerTest1 implements ServerListener { @Override public void onOpen(ChannelHandlerContext ctx) { System.out.println("Server onOpen"); byte[] msgBody = new byte[6]; msgBody[0] = 1; NetFunction.sendMsg(ctx.channel(), 1, Xxtea.encrypt(msgBody, Xxtea.getEncryptKey())); } @Override public void onMsg(ChannelHandlerContext ctx, int msgId, byte[] msgBody) { System.out.println("Server onMsg"); System.out.println(msgBody); // 解密 byte[] msgBodyRes = Xxtea.decrypt(msgBody, Xxtea.getEncryptKey()); System.out.println(Arrays.toString(Xxtea.decrypt(msgBody, Xxtea.getEncryptKey()))); try { CSLoginMsg.CS_LoginMsg cs_loginMsg = CSLoginMsg.CS_LoginMsg.parseFrom(msgBodyRes); System.out.println(cs_loginMsg.getChannelName()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } // 加密 NetFunction.sendMsg(ctx.channel(), 1, Xxtea.encrypt(msgBodyRes, Xxtea.getEncryptKey())); } @Override public void onClose(ChannelHandlerContext ctx) { } @Override public void onStop(boolean isSuccess) throws IOException { } }
// ClientListenerTest1
package ProtobufTest; import com.apanda.MessageCode; import com.apanda.db.util.Xxtea; import com.apanda.message.login.CSLoginMsg; import com.apanda.net.impl.ClientListener; import com.apanda.net.util.NetFunction; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import java.util.Arrays; public class ClientListenerTest1 implements ClientListener { private Channel channel; public Channel getChannel() { return channel; } public void setChannel(Channel channel) { this.channel = channel; } @Override public void onOpen(ChannelHandlerContext ctx) { System.out.println("Client onOpen"); // Protobuf测试 CSLoginMsg.CS_LoginMsg.Builder builder = CSLoginMsg.CS_LoginMsg.newBuilder(); builder.setChannelName("test"); CSLoginMsg.CS_LoginMsg cs_loginMsg = builder.build(); byte[] msgBody = cs_loginMsg.toByteArray(); NetFunction.sendMsg(ctx.channel(), MessageCode.CL_LoginMsg_CODE, Xxtea.encrypt(msgBody, Xxtea.getEncryptKey())); } @Override public void onMsg(ChannelHandlerContext ctx, int msgId, byte[] msgBody) { System.out.println("Client onMsg"); // 解密测试 System.out.println(Arrays.toString(Xxtea.decrypt(msgBody, Xxtea.getEncryptKey()))); } @Override public void onClose(ChannelHandlerContext ctx) { } @Override public void channelUnregistered(ChannelHandlerContext ctx) { } }