2013-04-24 92 views
-1

我跟着這個link來製作使用tomcat的websocket服務器。 當我試圖訪問WebSocket時,我的Firefox說它無法建立連接。Tomcat WebSocket錯誤

這裏是我的代碼

public class WsChatServlet extends WebSocketServlet 
{ 
private static final long serialVersionUID = 1L; 
private static ArrayList<MyMessageInbound> mmiList = new ArrayList<MyMessageInbound>(); 

@Override 
protected StreamInbound createWebSocketInbound(String string) { 
    System.out.println("CreateWebSocketInbound()"); 
    return new MyMessageInbound(); 
} 

private class MyMessageInbound extends MessageInbound 
{ 
    WsOutbound myoutbound; 

    @Override 
    protected void onBinaryMessage(ByteBuffer bb) throws IOException { 
     System.out.println("onBinaryMessage()"); 
    } 

    @Override 
    protected void onTextMessage(CharBuffer cb) throws IOException {    
     System.out.println("Accept Message : "+ cb); 
     for (MyMessageInbound mmib : mmiList) 
     { 
      CharBuffer buffer = CharBuffer.wrap(cb); 
      mmib.myoutbound.writeTextMessage(CharBuffer.wrap("Hello!")); 
      mmib.myoutbound.flush(); 
     } 
    } 

    @Override 
    protected void onOpen(WsOutbound outbound) { 
     System.out.println("onOpen()"); 
     try { 
      System.out.println("Open Client."); 
      this.myoutbound = outbound; 
      mmiList.add(this); 
      outbound.writeTextMessage(CharBuffer.wrap("Hello!")); 
     } catch (IOException ex) { 
      Logger.getLogger(WsChatServlet.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    @Override 
    protected void onClose(int status) { 
     System.out.println("Close Client."); 
     mmiList.remove(this); 
    } 
} 

這裏是服務器日誌

24 Apr 13 22:53:09 org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [WsChatServlet] in context with path [/HelloWebSocket] threw exception 
java.lang.ClassCastException: org.netbeans.modules.web.monitor.server.MonitorRequestWrapper cannot be cast to org.apache.catalina.connector.RequestFacade 
    at org.apache.catalina.websocket.WebSocketServlet.doGet(WebSocketServlet.java:126) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:662) 

如何解決這個問題,以及如何使內碼格式的日誌?

+0

如果您發佈的代碼,將是有益的你正在使用 – 2013-04-24 16:12:15

+0

是Tomcat版本?我建議使用apache tomcat 7.0.42,它可以通過WebSockets退出。 – Badal 2013-12-30 11:44:13

回答

0

檢查你的tomcat版本。 Websocket不是標準的,不同的服務器對websocket有不同的實現。我曾經使用過tomcat的websockets,它只能用於tomcat 7.0.27。即使7.0.29也沒有工作。所以我建議檢查一下你是否可以找到你的教程使用了哪個服務器,或者嘗試不同的版本。

+0

我正在使用tomcat 7.0.27 其實我試圖實現的是從使用JavaScript的mozilla插件與現有的java代碼進行通信的方式。你有沒有想過在不使用websocket的情況下實現這一點? – 2013-04-24 16:24:45

+0

使用websocket與Firefox擴展的好主意:) – 2013-04-24 17:43:54

+0

如果是單向通信,你可以使用ajax調用而不是websocket ... – 2013-04-24 17:44:38

1

這是Tomcat 7.0.27中的一個錯誤。我建議你使用7.0.37,或者使用類似Atmosphere框架(會從這樣的問題被屏蔽)

- Jeanfrancois

+0

使用像Atmosphere或[Cometd]這樣的框架+1(http://cometd.org )即使一旦jsr-356正式運行,您也可以從瀏覽器和服務器之間支持的最佳協議中獲益(spdy,websockets,甚至在今年晚些時候會登陸http/2) – 2013-04-24 19:48:11