webSocket通信

创建NettyServer类服务器配置

package com.gw.mesexecenter;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;

/**
 * 

* netty服务配置启动 *

* * @author: zsj * @dateTime: 2021/7/5 11:10 上午 **/
@Component @Slf4j public class NettyServer { /** * webSocket协议名 */ private static final String WEBSOCKET_PROTOCOL = "WebSocket"; /** * 端口号 */ @Value("${netty.server.port}") private int port; /** * webSocket路径 */ @Value("${netty.webSocket.path}") private String webSocketPath; /** * 自定义业务handler */ @Autowired private MyWebSocketHandler webSocketHandler; private EventLoopGroup bossGroup; private EventLoopGroup workGroup; private void start() throws InterruptedException { bossGroup = new NioEventLoopGroup(); workGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); // bossGroup辅助客户端的tcp连接请求,workGroup负责与客户端之前的读写操作 bootstrap.group(bossGroup, workGroup); // 设置NIO类型的channel bootstrap.channel(NioServerSocketChannel.class); // 设置日志打印级别 bootstrap.handler(new LoggingHandler(LogLevel.INFO)); // 设置监听端口 bootstrap.localAddress(new InetSocketAddress(port)); // 连接到达时会创建一个通道 bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // 流水线管理通道中的处理程序(Handler),用来处理业务 // webSocket协议本身是基于http协议的,所以这边也要使用http编解码器 ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new ObjectEncoder()); // 以块的方式来写的处理器 ch.pipeline().addLast(new ChunkedWriteHandler()); // 1、http数据在传输过程中是分段的,HttpObjectAggregator可以将多个段聚合 // 2、这就是为什么,当浏览器发送大量数据时,就会发送多次http请求 ch.pipeline().addLast(new HttpObjectAggregator(65536)); // 1、对应webSocket,它的数据是以帧(frame)的形式传递 // 2、浏览器请求时 ws://localhost:8888/xxx 表示请求的uri // 3、核心功能是将http协议升级为ws协议,保持长连接 ch.pipeline().addLast(new WebSocketServerProtocolHandler(webSocketPath, WEBSOCKET_PROTOCOL, true, 65536 * 10)); // 自定义的handler,处理业务逻辑 ch.pipeline().addLast(webSocketHandler); } }); // 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功 ChannelFuture channelFuture = bootstrap.bind().sync(); log.info("Server started and listen on:{}", channelFuture.channel().localAddress()); // 对关闭通道进行监听 channelFuture.channel().closeFuture().sync(); } /** * 释放资源 * * @throws InterruptedException */ @PreDestroy public void destroy() throws InterruptedException { if (bossGroup != null) { bossGroup.shutdownGracefully().sync(); } if (workGroup != null) { workGroup.shutdownGracefully().sync(); } } @PostConstruct() public void init() { //需要开启一个新的线程来执行netty server 服务器 new Thread(() -> { try { start(); } catch (InterruptedException e) { log.error(e.getMessage(), e); } }).start(); } }

2、配置NettyConfig

package com.gw.mesexecenter;


import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 

* netty服务配置 *

* * @author: zsj * @dateTime: 2021/07/05 10:52 **/
public class NettyConfig { /** * 定义一个channel组,管理所有的channel * GlobalEventExecutor.INSTANCE 是全局的事件执行器,是一个单例 */ private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); private NettyConfig() { } /** * 获取channel组 * * @return */ public static ChannelGroup getChannelGroup() { return channelGroup; } public static List<Channel> getChannelListByAttribute(Object attributeValue, AttributeKey<Object> attributeKey) { if (channelGroup.isEmpty()) { return Collections.emptyList(); } return channelGroup.stream().filter(c -> Objects.equals(attributeValue, c.attr(attributeKey).get())).collect(Collectors.toList()); } }

3、DefaultMessageProcessor

package com.gw.mesexecenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gw.scm.common.base.exception.BusinessException;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Objects;

import static com.caucho.hessian.io.HessianInputFactory.log;

/**
 * 

* 默认消息处理逻辑 *

* * @author: zsj * @dateTime: 2021/07/08 09:56 **/
@Component @Slf4j public class DefaultMessageProcessor implements TextWebSocketMessageProcess { //netty通道名称 public static final String ENERGY_CHANNEL_NAME = "ENERGY_CHANNEL_NAME"; @Override public void processMessage(ChannelHandlerContext ctx, WebSocketMessage msg) { try { if (Objects.equals(WebSocketMessageType.ENERGY_FLAG_PRINT.getType(), msg.getType())) { JSONObject obj = JSONObject.parseObject(msg.getContent()); AttributeKey<String> lineBodyKey = AttributeKey.valueOf(ENERGY_CHANNEL_NAME); ctx.channel().attr(lineBodyKey).set(obj.getString("vin")); // 成功处理消息,响应处理结果 msg.setStatus(WebSocketMessageProcessStatus.PROCESSING_COMPLETE.getStatus()); msg.setMsg("实时车辆推送成功!"); ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(msg, SerializerFeature.WriteMapNullValue))); log.info(String.format("实时车辆=[%s]建立链接", obj.getString("vin"))); } else { throw new BusinessException("消息类型无法处理,请联系系统管理员!"); } } catch (Exception e) { log.error(e.getMessage(), e); // 响应异常信息给客户端 WebSocketMessage errorMsg = new WebSocketMessage(); errorMsg.setType(WebSocketMessageType.EXCEPTION.getType()); errorMsg.setContent(Objects.nonNull(msg) ? msg.getContent() : ""); errorMsg.setMsg(MessageFormat.format("消息处理异常!消息:{0},原因:{1}", msg, e.getMessage())); errorMsg.setStatus(WebSocketMessageProcessStatus.PROCESSING_EXCEPTION.getStatus()); ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(errorMsg, SerializerFeature.WriteMapNullValue))); } } }

4、MyWebSocketHandler

package com.gw.mesexecenter;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 

* 自定义业务Handler *

* * @author: zsj * @dateTime: 2021/07/05 10:52 **/
@Component @ChannelHandler.Sharable @Slf4j public class MyWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { @Autowired private TextWebSocketMessageProcessorFactory messageProcessorFactory; @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { log.info("handlerAdded 被调用" + ctx.channel().id().asLongText()); // 添加到channelGroup 通道组 NettyConfig.getChannelGroup().add(ctx.channel()); ctx.channel().writeAndFlush(new TextWebSocketFrame("webSocket连接成功!")); } @Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { WebSocketMessage message = JSONObject.parseObject(msg.text(), WebSocketMessage.class); messageProcessorFactory.getMessageProcessor(message.getType()).processMessage(ctx, message); } @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { log.info("handlerRemoved 被调用" + ctx.channel().id().asLongText()); // 删除通道 NettyConfig.getChannelGroup().remove(ctx.channel()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { log.info("异常:{}", cause.getMessage()); // 删除通道 NettyConfig.getChannelGroup().remove(ctx.channel()); ctx.close(); } }

5、TextWebSocketMessageProcess

package com.gw.mesexecenter;

import io.netty.channel.ChannelHandlerContext;

/**
 * 

* *

* * @author: zsj * @dateTime: 2021/07/08 09:39 **/
public interface TextWebSocketMessageProcess { /** * @description:处理消息 * @return: void * @author: zsj * @dateTime: 2021/7/7 3:27 下午 **/ void processMessage(ChannelHandlerContext ctx, WebSocketMessage msg); }

6、TextWebSocketMessageProcessorFactory

package com.gw.mesexecenter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 

* TextWebSocketMessage处理chain *

* * @author: zsj * @dateTime: 2021/07/07 16:15 **/
@Component public class TextWebSocketMessageProcessorFactory implements Serializable { private static final long serialVersionUID = 2742511834182594757L; @Autowired private DefaultMessageProcessor defaultMessageProcessor; private static final Map<WebSocketMessageType, TextWebSocketMessageProcess> PROCESS_MAP = new HashMap<>(6); @PostConstruct public void initProcessMap() { PROCESS_MAP.put(WebSocketMessageType.DEFAULT, defaultMessageProcessor); //业务增加 //PROCESS_MAP.put(WebSocketMessageType.DEFAULT, defaultMessageProcessor); } public TextWebSocketMessageProcess getMessageProcessor(Integer type) { for (Map.Entry<WebSocketMessageType, TextWebSocketMessageProcess> entry : PROCESS_MAP.entrySet()) { if (Objects.equals(entry.getKey().getType(), type)) { return entry.getValue(); } } return PROCESS_MAP.get(WebSocketMessageType.DEFAULT); } }

7、WebSocketMessage

package com.gw.mesexecenter;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 

* webSocket消息对象 *

* * @author: zsj * @dateTime: 2021/07/07 15:34 **/
@Data @Accessors(chain = true) public class WebSocketMessage implements Serializable { private static final long serialVersionUID = -2992233853160597194L; /** * 消息标识(全局唯一) */ private String massageId; /** * 处理状态标识(0:未处理;1:已处理;2:处理时异常) */ private Integer status; /** * 消息类型(1:XX切换消息;2:XX切换消息;3:XX计时消息; * 4:XX) */ private Integer type; /** * 消息内容(json格式) */ private String content; /** * 提示信息 */ private String msg; }

8、WebSocketMessageProcessStatus

package com.gw.mesexecenter;

/**
 * 

* WebSocketMessage处理状态 *

* * @author: zsj * @dateTime: 2021/07/07 17:16 **/
public enum WebSocketMessageProcessStatus { NO_PROCESSING(0), PROCESSING_COMPLETE(1), PROCESSING_EXCEPTION(2); private Integer status; WebSocketMessageProcessStatus(Integer status) { this.status = status; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } }

9、WebSocketMessageType

package com.gw.mesexecenter;

/**
 * 

* webSocket消息类型 *

* * @author: zsj * @dateTime: 2021/07/07 14:43 **/
public enum WebSocketMessageType { EXCEPTION(2), ENERGY_FLAG_PRINT(1), DEFAULT(0); WebSocketMessageType(Integer type) { this.type = type; } private Integer type; public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } }

方法

    location ^~/webSocket {
        proxy_pass http://mesexe-center.1b8b432df2bfbd25-app-mes-exe-center:8888/webSocket; #websocket地址
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeou

你可能感兴趣的:(websocket,网络,服务器)