2014-05-22 114 views
5

我想建立一个java websocket服务器。我写了一个简单的服务器端点:Java websocket服务器给404

@ServerEndpoint(value = "/test") 
public class EndPoint { 
    static Queue<Session> queue = new ConcurrentLinkedQueue<Session>(); 

    public static void send(int a, int b) { 
     try { 
      for(Session session : queue) { 
       session.getBasicRemote().sendText("a = " + a + ",b=" + b); 
      } 
     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 

    @OnOpen 
    public void openConnection(Session session) { 
     queue.add(session); 
    } 

    @OnClose 
    public void closeConnection(Session session) { 
     queue.remove(session); 
    } 

    @OnError 
    public void error(Session session, Throwable t) { 
     queue.remove(session); 
    } 
} 

我的web.xml文件看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 


</web-app> 

当我打WS://本地主机:8080/WebSocket的-1.0 /测试从Javascript,我收到了404回应。我正在使用tomcat 8来部署这个。我在这里错过了什么?

回答

0

根据这个链接https://tyrus.java.net/documentation/1.4/index/websocket-api.html和我豁达的终点应与WS:URL

例如

@ServerEndpoint(value = "/test") 
ws://localhost:8080/test 

@ServerEndpoint(value = "/websocket-1.0//test") 
ws://localhost:8080/websocket-1.0/test 
+0

我仍然得到404上午我在web.xml中缺少任何东西? – coder

+0

你确定你在8080端口吗? –

+0

是的,它是端口8080 – coder

2

1)使用最新版本或Tomcat的的7.0.x的最新版本的Tomcat 8.0.x的

2)确保你的战争中只包含ServerEndpoint类和空的web.xml。 Tomcat的自动扫描并加载)与ServerEndpoint注释

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1" 
    metadata-complete="true"> 

</web-app> 

3注解类验证catalina.out中有如下日志味精,也没有例外。

org.apache.catalina.startup.HostConfig.deployWAR部署Web应用程序归档/usr/local/tomcat/apache-tomcat-8.0.12/webapps/.war

+0

谢谢,更改服务器到Tomcat8工作。 – prageeth