|
|
|
package com.crossoverjie.netty.action.server;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.crossoverjie.netty.action.init.HeartbeatInitializer;
|
|
|
|
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
|
|
|
|
import com.crossoverjie.netty.action.common.protocol.BaseRequestProto;
|
|
|
|
import com.crossoverjie.netty.action.init.HeartbeatInitializer;
|
|
|
|
import com.crossoverjie.netty.action.util.NettySocketHolder;
|
|
|
|
import com.crossoverjie.netty.action.vo.req.SendMsgReqVO;
|
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
import io.netty.channel.*;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelOption;
|
|
|
|
import io.netty.channel.EventLoopGroup;
|
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
...
|
...
|
@@ -91,4 +96,24 @@ public class HeartBeatServer { |
|
|
|
future.addListener((ChannelFutureListener) channelFuture ->
|
|
|
|
LOGGER.info("服务端手动发消息成功={}", JSON.toJSONString(customProtocol)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送 Google Protocol 编码消息
|
|
|
|
* @param sendMsgReqVO 消息
|
|
|
|
*/
|
|
|
|
public void sendGoogleProtoMsg(SendMsgReqVO sendMsgReqVO){
|
|
|
|
NioSocketChannel socketChannel = NettySocketHolder.get(sendMsgReqVO.getId());
|
|
|
|
|
|
|
|
if (null == socketChannel) {
|
|
|
|
throw new NullPointerException("没有[" + sendMsgReqVO.getId() + "]的socketChannel");
|
|
|
|
}
|
|
|
|
BaseRequestProto.RequestProtocol protocol = BaseRequestProto.RequestProtocol.newBuilder()
|
|
|
|
.setRequestId((int) sendMsgReqVO.getId())
|
|
|
|
.setReqMsg(sendMsgReqVO.getMsg())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
ChannelFuture future = socketChannel.writeAndFlush(protocol);
|
|
|
|
future.addListener((ChannelFutureListener) channelFuture ->
|
|
|
|
LOGGER.info("服务端手动发送 Google Protocol 成功={}", sendMsgReqVO.toString()));
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|