Java 关闭tomcat for websocket连接中的浏览器时刷新时在tomcat控制台中引发错误

Java 关闭tomcat for websocket连接中的浏览器时刷新时在tomcat控制台中引发错误,java,tomcat,websocket,angular5,Java,Tomcat,Websocket,Angular5,我是一个应用程序,我想保持心跳,以检查我的浏览器是否已连接。 为此,我实现了一个web套接字。 我在后端使用java,在前端使用angular 5 我的代码片段 @ServerEndpoint(value = "/endpoint", configurator = HttpSessionConfigurator.class) public class WebServer { @OnOpen public void onOpen(Session session, E

我是一个应用程序,我想保持心跳,以检查我的浏览器是否已连接。 为此,我实现了一个web套接字。 我在后端使用java,在前端使用angular 5

我的代码片段

    @ServerEndpoint(value = "/endpoint", configurator = HttpSessionConfigurator.class)
    public class WebServer {
      @OnOpen
  public void onOpen(Session session, EndpointConfig config) {
    log.debug("on open session: "+ session.getId());
    this.httpSession = (HttpSession) config.getUserProperties().get(IBClientConstants.HTTP_SESSION);
  }

  @OnClose
  public void onClose(Session session) { 
    log.debug("onClose session: "+ session.getId());
  }

  @OnMessage
  public void onMessage(String message, Session session) {
    String data = "success";//i send some data which is needed in UI
    session.getBasicRemote().sendText(data);
  }

  @OnError
  public void onError(Throwable t) {
    log.debug(t.getMessage());
  }
    }
我的边代码:

@Injectable()
export class WebSocketClient {
    private webSocket;

    private websocketUrl;

    constructor(private appConfig: AppConfig) {
        this.websocketUrl = appConfig.getBaseURLWithContextPath();
        if (this.websocketUrl) {
            this.websocketUrl = this.websocketUrl.replace("http", "ws") + "/endpoint"
        }
        this.webSocket = new WebSocket(this.websocketUrl);      
    }

    connect() {
        try {
            this.webSocket.onopen = (event) => {
                console.log('onopen::' + JSON.stringify(event, null, 4));
            }
            this.webSocket.onmessage = (event) => {
                var response = event.data;
                let jsonData = JSON.parse(response);
                //i use this data
            }
            this.webSocket.onclose = (event) => {
                console.log('onclose::' + JSON.stringify(event, null, 4));
            }
            this.webSocket.onerror = (event) => {
                console.log('onerror::' + JSON.stringify(event, null, 4));
            }

        } catch (exception) {
            console.error(exception);
        }
    }

    getStatus() {
        return this.webSocket.readyState;
    }

    ping() {
        if (this.webSocket.readyState == WebSocket.OPEN) {
            this.webSocket.send("ping");

        } else {
            console.error('webSocket is not open. readyState=' + this.webSocket.readyState);
        }
    }
    disconnect() {
        console.log("disconnect");
        if (this.webSocket.readyState == WebSocket.OPEN) {
            this.webSocket.close();

        } else {
            console.error('webSocket is not open. readyState=' + this.webSocket.readyState);
        }
    }

}
所有的功能都运行良好。但当我使用https连接时,这意味着我的websocket url变为wss://localhost:8443/InBetween/endpoint. 此时,若我在chrome中刷新浏览器,它会在控制台中引发异常。 以下是例外情况:

 Aug 13, 2018 12:11:07 PM
 org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer doClose
 INFO: Failed to close the ServletOutputStream connection cleanly
 java.io.IOException: An established connection was aborted by the
 software in your host machine
         at sun.nio.ch.SocketDispatcher.write0(Native Method)
         at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
         at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
         at sun.nio.ch.IOUtil.write(IOUtil.java:51)
         at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
         at org.apache.tomcat.util.net.SecureNioChannel.flush(SecureNioChannel.java:135)
         at org.apache.tomcat.util.net.SecureNioChannel.close(SecureNioChannel.java:370)
         at org.apache.tomcat.util.net.SecureNioChannel.close(SecureNioChannel.java:398)
         at org.apache.coyote.http11.upgrade.NioServletOutputStream.doClose(NioServletOutputStream.java:137)
         at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.close(AbstractServletOutputStream.java:100)
         at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.doClose(WsRemoteEndpointImplServer.java:138)
         at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.close(WsRemoteEndpointImplBase.java:597)
         at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.onWritePossible(WsRemoteEndpointImplServer.java:113)
我尝试在浏览器关闭时刷新时调用disconnect()。但这没有帮助。 我在IE中也进行了同样的测试,它没有给出任何错误

我的https连接器:

<Connector protocol="org.apache.coyote.http11.Http11NioProtocol" port="8443" maxThreads="200" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="some path" keystorePass="password"/>

问题是因为web套接字未正确断开连接。
我删除了this.webSocket=新的webSocket(this.websocketUrl),并添加一个单独的函数来连接,并在需要连接到web套接字的地方使用它。在浏览器刷新和关闭时也称为断开连接功能。

请使用代码格式显示错误消息和堆栈跟踪。更容易阅读。这次我修好了。好的,谢谢。在这个问题上有什么帮助吗?