Exception occur when refresh page in Tomcat JSR 356 websocket

465 views Asked by At

I have ServerEndpoint class:

@ServerEndpoint(value = "/websocket", configurator = ServletAwareConfig.class)
public class WebsocketEndpoint {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("OnOpen");
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("OnClose");
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("OnMessage");
    }

    @OnError
    public void onError(Throwable t) throws Throwable {
        System.out.println("OnError");
    }
}

When I refresh page, onError invoked and exception t contains next description: java.util.concurrent.ExecutionException: java.net.SocketException: Broken pipe

What can be reason?

1

There are 1 answers

0
Yikai Gong On

I meet the same exception when I use Chrome or Chromium to open the page. (It never occurs when I use Firefox). I'm not sure if you are using Chrome too. I guess it is because of the V8 engine.

My solution is to close the web socket before leaving the page:

window.addEventListener ("beforeunload", function(){ socket.close(); });

But I have not seen any exceptions like this when I use Tyrus as my web socket server. Hopes someone can show the direct reason of this problem. Thanks!