When does OnWebSocketClose fire in Jetty 9

2019-08-13 17:47发布

I have a simple embedded Jetty 9 websocket server. I have a method like this in my annotated websocket implementation:

@OnWebSocketClose
void onClose(int statusCode, String reason) {
    logger.info "Closed connection [${this}]"
    connectionManager.remove(this)
    Event closeEvent = commsEventFactory.buildCloseEvent(this, statusCode, reason)
    eventReceiver.postEvent(closeEvent)
}

I have a spock test which connects to the server using async-http-client. I get a successful connection and can send messages back and forth.

However, onClose() is never called (when I call async-http-client's websocket.close() or client.close() or even when the test JVM dies).

I expected the onClose to fire immediately when the far end of the socket disappears.

1条回答
Summer. ? 凉城
2楼-- · 2019-08-13 18:38

onClose() will fire to indicate that the local side websocket has changed state to closed.

It can result from:

  • A proper websocket close handshake has been received by remote endpoint (also known as a clean close)
  • Any protocol violation (in parsing or generating) that will attempt to issue a clean close (status code 1002). Note: due to the nature of this situation, this close might not be a clean close handshake.
  • A connection timeout, resulting in an abnormal (non-clean) close. (status code 1006)
  • Any read I/O exception on the socket, resulting in an abnormal (non-clean) close. (status code 1006)
  • Any write I/O exception on the socket, resulting in an abnormal (non-clean) close. (status code 1006)
查看更多
登录 后发表回答