[fix] add server send message to client

This commit is contained in:
wujiawei
2024-01-23 14:50:56 +08:00
parent be9cf25233
commit 2ec8dad5ef
14 changed files with 374 additions and 6 deletions

View File

@@ -104,4 +104,10 @@ public interface NettyClientStateApplication {
Result<NettyClientState> remove(NettyClientStateRemoveCommand nettyClientStateRemoveCommand);
/**
* 通过客户端心跳通道发送客户端请求
* @param nettyClientMessageCommand 发送请求到客户端
* @return {@link Result<Void>}
*/
Result<Void> sendMessage2HeartbeatClient(NettyClientMessageCommand nettyClientMessageCommand);
}

View File

@@ -0,0 +1,36 @@
package wu.framework.lazy.cloud.heartbeat.server.application.command.netty.client.state;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
import wu.framework.lazy.cloud.heartbeat.common.enums.NettyClientStatus;
import java.time.LocalDateTime;
/**
* describe 发送请求到客户端
*
* @author Jia wei Wu
* @date 2023/12/27 03:46 下午
* @see com.wu.framework.inner.lazy.persistence.reverse.lazy.ddd.DefaultDDDLazyRemoveCommand
**/
@Data
@Accessors(chain = true)
@Schema(title = "netty_client_message_command", description = "客户端状态")
public class NettyClientMessageCommand {
/**
* 客户端ID
*/
@Schema(description = "客户端ID", name = "clientId", example = "")
private String clientId;
/**
* 发送的消息
*/
@Schema(description = "发送的消息", name = "message", example = "")
private String message;
}

View File

@@ -2,7 +2,11 @@ package wu.framework.lazy.cloud.heartbeat.server.application.impl;
import com.wu.framework.response.ResultFactory;
import io.netty.channel.Channel;
import wu.framework.lazy.cloud.heartbeat.common.ChannelContext;
import wu.framework.lazy.cloud.heartbeat.common.MessageType;
import wu.framework.lazy.cloud.heartbeat.common.NettyProxyMsg;
import wu.framework.lazy.cloud.heartbeat.server.application.NettyClientStateApplication;
import wu.framework.lazy.cloud.heartbeat.server.application.assembler.NettyClientStateDTOAssembler;
import wu.framework.lazy.cloud.heartbeat.server.application.command.netty.client.state.NettyClientStateStoryCommand;
@@ -15,6 +19,7 @@ import com.wu.framework.inner.lazy.database.expand.database.persistence.domain.L
import com.wu.framework.response.Result;
import jakarta.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
/**
@@ -143,4 +148,28 @@ public class NettyClientStateApplicationImpl implements NettyClientStateApplicat
return nettyClientStateRepository.remove(nettyClientState);
}
/**
* 通过客户端心跳通道发送客户端请求
*
* @param nettyClientMessageCommand 发送请求到客户端
* @return {@link Result<Void>}
*/
@Override
public Result<Void> sendMessage2HeartbeatClient(NettyClientMessageCommand nettyClientMessageCommand) {
// 获取客户端ID
String clientId = nettyClientMessageCommand.getClientId();
String message = nettyClientMessageCommand.getMessage();
ChannelContext.ClientChannel clientChannel = ChannelContext.get(clientId);
if(clientChannel==null){
return ResultFactory.errorOf("客户端:"+clientId+"不存在");
}
// 发送消息到客户端
Channel channel = clientChannel.getChannel();
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
nettyProxyMsg.setClientId("服务端");
nettyProxyMsg.setData(message.getBytes(StandardCharsets.UTF_8));
nettyProxyMsg.setType(MessageType.DISTRIBUTE_SINGLE_CLIENT_MESSAGE);
channel.writeAndFlush(nettyProxyMsg);
return ResultFactory.successOf();
}
}

View File

@@ -136,4 +136,19 @@ public class NettyClientStateProvider {
public Result<NettyClientState> remove(@ModelAttribute NettyClientStateRemoveCommand nettyClientStateRemoveCommand){
return nettyClientStateApplication.remove(nettyClientStateRemoveCommand);
}
/**
* describe 通过客户端心跳通道发送客户端请求
* @param nettyClientMessageCommand 发送请求到客户端
* @return {@link Result<NettyClientState>}
* @author Jia wei Wu
* @date 2023/12/27 03:46 下午
**/
@Operation(summary = "通过客户端心跳通道发送客户端请求")
@PostMapping("/sendMessage2HeartbeatClient")
public Result<Void> sendMessage2HeartbeatClient(@RequestBody NettyClientMessageCommand nettyClientMessageCommand){
return nettyClientStateApplication.sendMessage2HeartbeatClient(nettyClientMessageCommand);
}
}