2014-05-22 115 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