Stomp over socket using sockjs can't connect with Spring 4 WebSocket

前端 未结 4 2054
南方客
南方客 2020-12-06 13:51

Trying to use Spring 4 WebSocket with STOMP over socket using sockjs. And i faced a problem.

My configuration:

websocket.xml - part of spring context

相关标签:
4条回答
  • 2020-12-06 14:10

    I think your issue can be the same of this one and its accepted answer can apply.

    In short: check how you mapped your DispatcherServlet. For your endpoint URL to be http://localhost:8080/ws the DispatcherServlet and your application contexte root should be set as "/".

    It should be something like this, in your web.xml:

    <servlet>
      <servlet-name>WebSocketDispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
      <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
      <servlet-name>WebSocketDispatcher</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    This or you need to include context-root and dispatcher mapping in your end-point URL:

    http://<server>:<port>/<context-root>/<dispatcher-mapping>/ws
    

    I hope it can help you.

    Regards.

    Heleno

    0 讨论(0)
  • 2020-12-06 14:16

    I arrived on this page and - thanks to Boris Accepted Answer was motivated to reconsider the java approach as opposed to the xml approach which was causing the - /info=34424 with 404 error...whoops lost connection - I have Spring 4.2 in my project and many SockJS Stomp implementations usually work well with Spring Boot implementations. This implementation from Baeldung worked(for me without changing from Spring 4.2 to 5 or Boot). After Using the dependencies mentioned in his blog, it still gave me ClassNotFoundError. I added the below dependency to fix it.

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
    

    Baeldung's implementation curiously does not make any such calls

    flow/websocket/add/info?t=1540813753999
    

    What it does (on send and receive) is below. I am only pasting it in case people well-versed with these libraries can further add insights on this forum.

     >>> SEND
    destination:/app/chat
    content-length:38
    
    {"from":"nicholas","text":"try again"}
    
    <<< MESSAGE
    destination:/topic/messages
    content-type:application/json;charset=UTF-8
    subscription:sub-0
    message-id:m3p096zk-11
    content-length:53
    
    {"from":"nicholas","text":"try again","time":"13:46"}
    
    0 讨论(0)
  • 2020-12-06 14:18

    I follow Boris The Spider advice and i started use Java Configuration (AppConfig and WebInit files) instead of XML configuration files. When i finished migration - i tried websockets - and it is works! I thought that the main problem was in XML configuration for WebSocket.

    0 讨论(0)
  • 2020-12-06 14:20

    firstly, you have set destination prefix with app, so you must add 'app' into your request url. e.g. if i wanted to request for '/ws', i should create SockJS with path '/server/app/ws', also you can code with var socket = new SockJS("<c:url value='/server/app/ws'/>") and you know the 'server' is your project name over springmvc.

    secondly, you must add the path '/app/*' to org.springframework.web.servlet.DispatcherServlet so as to avoid the path with prefix '/app' intercepted by interceptor defined in web.xml.

    0 讨论(0)
提交回复
热议问题