记录linux websocket握手时间过长问题

1 握手拦截器 打印时间过长

bash 复制代码
@Service
@Slf4j
public class SocketInterceptor implements HandshakeInterceptor {

    /**
     * socket握手拦截
     *
     * @param request
     * @param response
     * @param wsHandler
     * @param attributes
     * @return boolean
     * @throws Exception
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
//        if(!SystemConstants.SYSTEM_INIT_SUCCESS){
//            return false;
//        }
        log.error("开始握手 {}", LocalDateTime.now());
        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        //获取token
        String token = servletRequest.getHeader("authorization");
        if (StringUtils.isBlank(token)){
            return false;
        }
        //检查是否登录了
        SystemUserDetails userTokenInfoVo = AuthUtil.getLoginUser();
        if(userTokenInfoVo==null){
            return false;
        }
        //终端类型
        String clientType = SocketUtil.getClientTypeByRequest(servletRequest);
        //当前会话token
        attributes.put("authorization", token);
        //请求终端id
        attributes.put(SocketUtil.USER_ID, userTokenInfoVo.getUserId());
        //当前链接客户端类型
        attributes.put(SocketUtil.CLIENT_TYPE, StringUtils.isBlank(clientType)? "SYSTEM_ADMIN":clientType);
        //获取客户端ip
        String clientIp = IpUtils.getIpAddr(servletRequest);
        attributes.put(SocketUtil.CLIENT_IP, clientIp);
        log.error("开始握手完毕 {}", LocalDateTime.now());
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        log.error("afterHandshake {}", LocalDateTime.now());
    }
}

2 解决方案

原因分析:

找到服务器上的进程名

jps -l

使用jstack 进程号

查看自己服务堆栈的线程

3 解决方案

在系统hosts文件中增加本地ip地址与主机名的对应项

  1. ifconfig 查看本机 ip

  2. 修改hosts文件 sudo vim /etc/hosts

  3. 在文件中追加 192.168.0.224 myserver --myserver 这个随便起名

  4. 测试 ping myserve

再次进行websocket连接

完工 记录一下

相关推荐
一直跑17 小时前
查看显卡驱动版本,查看哪个用户使用显卡(GPU)进程
linux·服务器
滴水之功17 小时前
Windows远程桌面(非图形化界面)连接Ubuntu22.04
linux
daqinzl17 小时前
JavaScript连接WebSocket
javascript·websocket
借你耳朵说爱你18 小时前
在Linux上挂载磁盘
linux
小成2023032026518 小时前
Linux高级
linux·开发语言
ICT系统集成阿祥18 小时前
Linux运维最万能的三条指令
linux·运维·服务器
CAU界编程小白18 小时前
Linux系统编程系列之模拟文件操作
linux·算法
chenyuhao202419 小时前
Linux网络编程:数据链路层
linux·运维·网络
QT 小鲜肉19 小时前
【Linux命令大全】002.文件传输之uupick命令(实操篇)
linux·运维·服务器·chrome·笔记
QT 小鲜肉19 小时前
【Linux命令大全】003.文档编辑之colrm命令(实操篇)
linux·运维·服务器·chrome·笔记