halber
halber

Reputation: 197

Websocket session.close in @OnOpen results in server freeze

I have a problem with a websocket in Java EE 7. I tried to reject a connection in the @OnOpen annotated method. To close the connection I use session.close(). But after a few times the sever freeze.

Here is a short example. This example is based on the netbeans WebSocket Echo Sample Application. After a few message, the server freezes. No Exception is thrown.

Do I miss something? How do I reject a connection correctly?

I use Glassfish 4.0.

@ServerEndpoint("/echo")
public class EchoEndpoint {

  @OnMessage
  public String echo(String message) {
    return message;
  }

  @OnOpen
  public void onOpen(Session session) {
    try {
      session.close();
    } catch (IOException ex) {
      Logger.getLogger(EchoEndpoint.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

Upvotes: 1

Views: 1155

Answers (2)

Pavel Bucek
Pavel Bucek

Reputation: 5324

This was a bug in TYRUS 1.0, see TYRUS-212. I recommend using Tyrus 1.3.3 (latest stable version), it contains significant stability and performance improvements compared to Tyrus 1.0.

Upvotes: 2

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49452

That would be a bug in the websocket implementation you are using (Glassfish).

It is allowed per the WebSocket protocol, and the javax.websocket (JSR-356) API spec.

That behavior is well supported on other server side implementations of javax.websocket (like Eclipse Jetty 9.1 and Apache Tomcat 8.x)

Upvotes: 1

Related Questions