正在开发中
redis 文档
基本结构
common
core
server
client
启动
com.gitee.deament.server.MyServer --》右键启动
核心类
com.gitee.deament.server.config.SystemConfig
包含了数据和非数据的信息存储
基本架构
netty
辨别会话 channel
public class MyServerInitailizer extends ChannelInitializer {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline channelPipeline= socketChannel.pipeline();
channelPipeline.addLast(new HttpServerCodec());
channelPipeline.addLast(new ChunkedWriteHandler());
channelPipeline.addLast(new HttpObjectAggregator(8192));
channelPipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
channelPipeline.addLast(new CommandHander());// 核心处理类
}
}
编写一个命令处理
@CliHander(command = "HDEL")
public class HDELcli extends AbstractCli {
@Override
public void executeCli(ChannelHandlerContext channelHandlerContext, String command) {
List values = getValues(command);
if (values.size() < 3) {
channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame("ERR wrong number of arguments for 'HDEL' command"));
}
int db = ConnectData.SELECT_DB_STORAGE.getUseDB(channelHandlerContext.channel().id().asLongText());
Value> hashData = SystemConfig.dbData.getHashData(db, values.get(0));
StringBuilder msg = new StringBuilder();
for (int i = 2; i < values.size(); i++) {
String result = hashData.getValue().remove(values.get(i));
if (result == null) {
msg.append("0\r\n");
} else {
msg.append("1\r\n");
}
}
channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame(msg.toString()));
}
}
@CliHander 接收命令的首个单词 如 命令 HDEL hash key ,以第一个单词作为命令处理分类,
AbstractCli 抽象的方法,需要实现执行命令的具体步骤
ConnectData 类是连接的信息和数据无关
SystemConfig.dbData 存储数据的地方
结构与redis服务器类似 先要AUTH 执行认证密码后 再执行命令操作
测试(临时)
skyCloud\server\src\test\resources\static\webSocketTest.html(需启动后端)