WebSocket handshake: Unexpected response code: 404

后端 未结 4 1592
予麋鹿
予麋鹿 2021-02-19 06:07

Am writing my first websocket program and am getting \"WebSocket handshake: Unexpected response code: 404\", error while loading the webpage.

I am using

相关标签:
4条回答
  • 2021-02-19 06:32

    It's because of the issue of /info=34424 - with 404 error - that I had to abandon using the xml approach suggested at other places. 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). 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)
  • 2021-02-19 06:33

    Thank you guys for your suggestion, I found the answer.

    The code I copied is from http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html site.

    The problem was the url as mentioned in the js file and the project name they are proposing is WebsocketHome. I had changed the project name to Websocket thus my url should be ws://localhost:8080/Websocket/actions.

    Thanks for your support.

    0 讨论(0)
  • 2021-02-19 06:41

    Actually, the problem here is case-sensitivity in the URLs. You did not need to change the project name. Just changing the Websocket URL in JavaScript file to

    ws://localhost:8080/WebSocketHome/actions 
    

    (with capital S, as in the project name) would have solved the problem. In your case, changing both of them removed the case inconsistency, so it worked.

    0 讨论(0)
  • 2021-02-19 06:55

    My guess is that you are trying to contact the websocket with a normal browser. That is not allowed and gives a 404 error. You need to use a script or curl to address websockets.

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