首页 > 解决方案 > 转换为 Maven 项目时 Java Web Socket 不起作用

问题描述

我正在创建动态网络项目。

我有课:

@ServerEndpoint("/websocketendpoint")
public class WsServer {
    @OnOpen
    public void onOpen( Session session){
        System.out.println("Open Connection ...");


          PushTimeService.initialize();

          PushTimeService.add(session);

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

    @OnMessage
    public String onMessage(String message){
        System.out.println("Message from the client: " + message);
        String echoMsg = "Echo from the server : " + message;
        return echoMsg;
    }
    @OnError
    public void onError(Throwable e){
        e.printStackTrace();
    }
}

使用 tomcat 运行项目,我可以从 JS 文件访问 URI:

ws://localhost:8080/WebSocketStream/websocketendpoint

但是我需要在项目中添加一些额外的 *.jar 并添加一些ReastEASY方法,因此我需要将其转换为 Maven 项目。

我正在用 Eclipse 做,转换为 maven 项目。但是在构建阶段之后,我无法访问 URI,得到 404 代码。

我尝试在 URI 中添加版本号,如下所示: ws://localhost:8080/WebSocketStream-1.0/websocketendpoint但结果相同,我无法访问它。

请建议。

标签: javaeclipsemavenwebsocket

解决方案


推荐阅读