-1

我按照这个链接使用 tomcat 制作 websocket 服务器。当我尝试访问 websocket 时,我的 firefox 说它无法建立连接。

这是我的代码

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)

如何解决这个问题以及如何使日志内码格式?

4

2 回答 2

1

这是 Tomcat 7.0.27 中的一个错误。我建议你使用 7.0.37,或者使用像Atmosphere这样的框架(会屏蔽此类问题)

——让弗朗索瓦

于 2013-04-24T19:40:31.450 回答
0

检查您的 Tomcat 版本。Websockets 不是标准的,不同的服务器对 websocket 有不同的实现。我曾经将 websockets 与 tomcat 一起使用,它过去只能与 tomcat 7.0.27 一起使用。即使使用 7.0.29,它也无法正常工作。因此,我建议检查您是否可以找到您的教程使用的服务器或尝试使用不同的版本。

于 2013-04-24T16:15:00.710 回答