1
回答
tomcat重启时websocket连接关闭,websocket总是报错Unable to write the complete message as the WebSocket connection has been closed  
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   

求各路大神告知..这是什么原因?

我在修改代码后后,eclipse自动编译了,用是tomcat8,jdk1.8,老是报以下错误:

[INFO]-[Thread: ContainerBackgroundProcessor[StandardEngine[Catalina]]]-[com.alibaba.druid.pool.DruidDataSource.close()]: {dataSource-1} closed
java.io.IOException: java.util.concurrent.ExecutionException: java.io.IOException: Unable to write the complete message as the WebSocket connection has been closed
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:282)
    at org.apache.tomcat.websocket.WsSession.sendCloseMessage(WsSession.java:584)
    at org.apache.tomcat.websocket.WsSession.doClose(WsSession.java:488)
    at org.apache.tomcat.websocket.WsSession.close(WsSession.java:455)
    at org.apache.tomcat.websocket.WsWebSocketContainer.destroy(WsWebSocketContainer.java:929)
    at org.apache.tomcat.websocket.server.WsServerContainer.destroy(WsServerContainer.java:285)
    at org.apache.tomcat.websocket.server.WsContextListener.contextDestroyed(WsContextListener.java:48)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4857)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5478)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3824)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5616)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Unable to write the complete message as the WebSocket connection has been closed
    at org.apache.tomcat.websocket.FutureToSendHandler.get(FutureToSendHandler.java:102)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:275)
    ... 17 more
Caused by: java.io.IOException: Unable to write the complete message as the WebSocket connection has been closed
    at org.apache.tomcat.websocket.WsSession.registerFuture(WsSession.java:658)
    at org.apache.tomcat.websocket.FutureToSendHandler.get(FutureToSendHandler.java:92)
    ... 18 more
发生错误
有连接关闭

用到websocket的代码:

@ServerEndpoint("/websocket")
public class WebSocketTest {  
    //连接池,用来保存每个客户端连接的对象  
    private static List<WebSocketTest> msgList   
        = new ArrayList<WebSocketTest>();  
      
    //与某个客户端的连接会话,需要通过它来给客户端发送数据  
    private Session session;  
      
    /** 
     * 连接建立成功调用的方法 
     * @param session  可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 
     */  
    @OnOpen  
    public void onOpen(Session session){  
        this.session = session;  
        msgList.add(this);  
        System.out.println("有新连接加入");  
    }  
      
    /** 
     * 连接关闭调用的方法 
     */  
    @OnClose  
    public void onClose(){  
        msgList.remove(this);  
        System.out.println("有连接关闭");  
    }  
      
    /** 
     * 收到客户端消息后调用的方法 
     * @param message 客户端发送过来的消息 
     * @param session 可选的参数 
     */  
    @OnMessage  
    public void onMessage(String message, Session session) {  
        System.out.println("来自客户端的消息:" + message);  
     
        for(WebSocketTest item: msgList){  
            try {  
                
                item.sendMessage(message);  
            } catch (IOException e) {  
                e.printStackTrace();  
                continue;  
            }  
        }  
    }  
      
    /** 
     * 发生错误时调用 
     * @param session 
     * @param error 
     */  
    @OnError  
    public void onError(Session session, Throwable error){  
        System.out.println("发生错误");  
        error.printStackTrace();  
    }  
      
    /** 
     * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。 
     * @param message 
     * @throws IOException 
     */  
    public void sendMessage(String message) throws IOException{  
        this.session.getBasicRemote().sendText(message);//发送消息给客户端  
        //this.session.getAsyncRemote().sendText(message);  
    }  
}  

 

 

 

 

举报
顶部