Dmitry
Dmitry

Reputation: 55

Intercept Akka HTTP WebSocket events on socket close in Scala

I'm using Scala with Akka HTTP.

I have a server backend application that's based on Akka HTTP. Incoming WebSocket messages are handled with handleWebSocketMessages:

/**
  * Route for WebSocket request
  */
private val webSocketRoute: Route = pathSuffix(Constants.WSPROXY_WEBSOCKET_PATH_SUFFIX) {
    LOGGER.debug("() Web socket route")
    handleWebSocketMessages(wsRouteImpl)
}

/**
  * This method calls all registered handlers.
  *
  * @return flow for handleWebSocketMessages method
  */
private def wsRouteImpl: Flow[Message, Message, Any] = {
    LOGGER.debug("() wsRouteHandling")
    numOfClients += 1

    LOGGER.info(s"Client has connected, current number of clients: $numOfClients")

    var flow = Flow[Message].mapConcat {
  // Call specific handlers depending on message type
  ...
}

My WebSocket clients establish two-way communication connections with keep-alive.

Binding is done with:

val binding = Http().bindAndHandle(webSocketRoute, config.host, config.port)

The problem is that I need to inject a callback for a closed socket (for example, if a client has disconnected) and decrease current number of clients, but I can't find any entry points for this.

Is it possible to catch some kind of event on socket close?

Upvotes: 2

Views: 1049

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

Use watchTermination:

val numOfClients = new java.util.concurrent.atomic.AtomicInteger(0)

private val webSocketRoute: Route = pathSuffix(Constants.WSPROXY_WEBSOCKET_PATH_SUFFIX) {
  LOGGER.debug("() Web socket route")

  val wsFlow: Flow[Message, Message, Any] =
    wsRouteImpl.watchTermination() { (_, fut) =>
      numOfClients.incrementAndGet()
      LOGGER.info(s"Client has connected. Current number of clients: $numOfClients")

      fut onComplete {
        case Success(_) =>
          numOfClients.decrementAndGet()
          LOGGER.info(s"Client has disconnected. Current number of clients: $numOfClients")
        case Failure(ex) =>
          numOfClients.decrementAndGet()
          LOGGER.error(s"Disconnection failure (number of clients: $numOfClients): $ex")
      }
    }

  handleWebSocketMessages(wsFlow)
}

private def wsRouteImpl: Flow[Message, Message, Any] = ???

Upvotes: 5

Related Questions