Websocket以GET方式 ?xxx&xxx 接收可选参数

本来用websocket使用注解的方式接受参数,但是有一个需求,有一个参数可能传又可能不传,本人不才,找了很多方式都未能以注解的方式实现,所以还是用传统拦截器的方式获取参数,主要配置如下:

拦截器类

@Component
public class WebSocketInterceptor extends HttpSessionHandshakeInterceptor {
    private Logger log = LoggerFactory.getLogger(this.getClass());


    /**
     * 握手后
     *
     * @param request
     * @param response
     * @param wsHandler
     * @param ex
     */
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
    }

    /**
     * TODO 描述该方法的实现功能.
     *
     * @see org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor#beforeHandshake(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.WebSocketHandler, java.util.Map)
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
            //获取参数
            String id = serverHttpRequest.getServletRequest().getParameter("id");
            String name = serverHttpRequest.getServletRequest().getParameter("name");
            log.info("拦截入参{}", id);
            attributes.put("id", id);
            if (name == null) {
            // 如果参数为存0 然后判断这个参数是否为0就可以了
                attributes.put("name", "0");
            } else {
                attributes.put("name", name);
            }

            HttpSession httpSession = serverHttpRequest.getServletRequest().getSession(false);
            if (httpSession != null) {
                log.debug("HttpSessionId:" + httpSession.getId());
                attributes.put("currHttpSession", httpSession);
            } else {
                log.debug("httpsession is null");
            }
        }

        return true;
    }

    @Override
    public Collection<String> getAttributeNames() {
        // TODO Auto-generated method stub
        return super.getAttributeNames();
    }

    @Override
    public boolean isCopyAllAttributes() {
        // TODO Auto-generated method stub
        return super.isCopyAllAttributes();
    }

    @Override
    public boolean isCopyHttpSessionId() {
        // TODO Auto-generated method stub
        return super.isCopyHttpSessionId();
    }

    @Override
    public boolean isCreateSession() {
        // TODO Auto-generated method stub
        return super.isCreateSession();
    }

    @Override
    public void setCopyAllAttributes(boolean copyAllAttributes) {
        // TODO Auto-generated method stub
        super.setCopyAllAttributes(copyAllAttributes);
    }

    @Override
    public void setCopyHttpSessionId(boolean copyHttpSessionId) {
        // TODO Auto-generated method stub
        super.setCopyHttpSessionId(copyHttpSessionId);
    }

    @Override
    public void setCreateSession(boolean createSession) {
        // TODO Auto-generated method stub
        super.setCreateSession(createSession);
    }

}

websocket配置类

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // BidSocketCoreService 这个是websocket接收操作类  路径是与前台一致的路径
        registry.addHandler(new BidSocketCoreService(), "/webSocket/socketDetail")
            // WebSocketInterceptor就是刚才配置的拦截器类 
            // setAllowedOrigins("*") 这个要加上 支持跨域
            .addInterceptors(new WebSocketInterceptor()).setAllowedOrigins("*");
    }

}

接下来BidSocketCoreService类需要继承 WebSocketHandler类 实现接口,类上加上@Service注解,其他的就和注解方式一样了

然后在spring-servlet里加上拦截

	<mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**" />
                <mvc:exclude-mapping path="/webSocket/**"/>
            </mvc:interceptor>
    </mvc:interceptors>

就可以了

前端访问ws://xxxxxx/webSocket/socketDetail?id=xxx&name=xxx就可以获取了,name可以传也可以不传

有什么问题互相进步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值