springboot websocket error 200错误:Error during WebSocket handshake: Unexpected response code: 200

﹏ヽ暗。殇╰゛Y 2022-12-26 10:16 214阅读 0赞

Error during WebSocket handshake: Unexpected response code: 200

解决方案:

1、https的需要使用 wss://

2、(非nginx跳过)nginx代理需要配置持续连接。

  1. 这里还要注意一下,如果你连接服务器携带了参数,如 **/ws/\{id\}** ,下面这个配置也是同样生效的!不需要另外获取这个 **\{id\}**,再转发到 **127.0.0.1/ws/\{id\}**。还有可能出现的问题是:这个 **@ServerEndpoint(value = "/ws/\{id\}")** 中的 /ws/id 被另外一个 RequestMapping 使用了,程序中是不会报错的,但是在 nginx 中的配置是不行的,默认会转发到 RequestMapping 的路径,所以如下配置在这个情况是不生效的。
  2. location /ws {
  3. proxy_pass http://127.0.0.1:5011/ws/;
  4. proxy_http_version 1.1;
  5. proxy_set_header Upgrade $http_upgrade;
  6. proxy_set_header Connection "upgrade";
  7. }

3、在系统中使用了权限框架,没有把 Websocket 的连接地址放出来。

4、(非Stomp+Websocket跳过)在继承 WebConfigurer 的类中注册拦截器时,多加了 .withSockJS() 方法,删除如下的 .withSockJS() 方法。

  1. public void addInterceptors(InterceptorRegistry registry) {
  2. registry.addHandler(new CustomerWebSocketHandler(),"/chat")
  3. .addInterceptors(new CustomerWebSocketInterceptor()).withSockJS();
  4. }

5、补充一点:在 WebsocketEndPoint 的类中是无法注入 Service或Component的,需要使用 SpringUtils(统称) 来获取容器的上下文环境,这个工具类送给大家:

  1. import org.springframework.aop.framework.AopContext;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  5. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.ApplicationContextAware;
  8. import org.springframework.stereotype.Component;
  9. /**
  10. * spring工具类 方便在非spring管理环境中获取bean
  11. *
  12. * @author mrdjun
  13. */
  14. @Component
  15. public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
  16. /**
  17. * Spring应用上下文环境
  18. */
  19. private static ConfigurableListableBeanFactory beanFactory;
  20. private static ApplicationContext applicationContext;
  21. @Override
  22. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  23. SpringUtils.beanFactory = beanFactory;
  24. }
  25. @Override
  26. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  27. SpringUtils.applicationContext = applicationContext;
  28. }
  29. /**
  30. * 获取对象
  31. *
  32. * @param name
  33. * @return Object 一个以所给名字注册的bean的实例
  34. * @throws org.springframework.beans.BeansException
  35. */
  36. @SuppressWarnings("unchecked")
  37. public static <T> T getBean(String name) throws BeansException {
  38. return (T) beanFactory.getBean(name);
  39. }
  40. /**
  41. * 获取类型为requiredType的对象
  42. *
  43. * @param clz
  44. * @return
  45. * @throws org.springframework.beans.BeansException
  46. */
  47. public static <T> T getBean(Class<T> clz) throws BeansException {
  48. T result = (T) beanFactory.getBean(clz);
  49. return result;
  50. }
  51. /**
  52. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
  53. *
  54. * @param name
  55. * @return boolean
  56. */
  57. public static boolean containsBean(String name) {
  58. return beanFactory.containsBean(name);
  59. }
  60. /**
  61. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
  62. *
  63. * @param name
  64. * @return boolean
  65. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  66. */
  67. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
  68. return beanFactory.isSingleton(name);
  69. }
  70. /**
  71. * @param name
  72. * @return Class 注册对象的类型
  73. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  74. */
  75. public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
  76. return beanFactory.getType(name);
  77. }
  78. /**
  79. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
  80. *
  81. * @param name
  82. * @return
  83. * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
  84. */
  85. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
  86. return beanFactory.getAliases(name);
  87. }
  88. /**
  89. * 获取aop代理对象
  90. *
  91. * @param invoker
  92. * @return
  93. */
  94. @SuppressWarnings("unchecked")
  95. public static <T> T getAopProxy(T invoker) {
  96. return (T) AopContext.currentProxy();
  97. }
  98. /**
  99. * 获取当前的环境配置,无配置返回null
  100. *
  101. * @return 当前的环境配置
  102. */
  103. public static String[] getActiveProfiles() {
  104. return applicationContext.getEnvironment().getActiveProfiles();
  105. }
  106. /**
  107. * 获取当前的环境配置,当有多个环境配置时,只获取第一个
  108. *
  109. * @return 当前的环境配置
  110. */
  111. public static String getActiveProfile() {
  112. final String[] activeProfiles = getActiveProfiles();
  113. return activeProfiles != null && activeProfiles.length > 0 ? activeProfiles[0] : null;
  114. }
  115. }

使用方式: SpringUtils.getBean(IXxxService.class).save(log); 这里的 IXxxService.class 可以是接口类(但必须是容器中的Bean),也可以是接口的实现类。

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读