WebSocket handshake: Unexpected response code: 404

程序运行的环境

程序代码

import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/websocket")
public class WebSocketTest {

    @OnMessage
    public void onMessage(String message, Session session) 
        throws IOException, InterruptedException {
        
        // Print the client message for testing purposes
        System.out.println("Received: " + message);
        
        // Send the first message to the client
        session.getBasicRemote().sendText("This is the first server message");
        
        // Send 3 messages to the client every 5 seconds
        int sentMessages = 0;
        while(sentMessages < 3){
            Thread.sleep(5000);
            session.getBasicRemote().
                sendText("This is an intermediate server message. Count: " 
                    + sentMessages);
            sentMessages++;
        }
        
        // Send a final message to the client
        session.getBasicRemote().sendText("This is the last server message");
    }
    
    @OnOpen
    public void onOpen () {
        System.out.println("Client connected");
    }

    @OnClose
    public void onClose () {
        System.out.println("Connection closed");
    }
}

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>Simple web application</display-name>
    <welcome-file-list>
    <!-- 首页加载action <welcome-file>index.action</welcome-file> -->
    <welcome-file>page.html</welcome-file>
  </welcome-file-list>
</web-app>

chrome版本
图片描述

websocket 感觉该具备的条件都具备了,但就是404

阅读 13.1k
1 个回答

求大神来回答

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进