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?