import org.jetbrains.concurrency.Promise
import org.jetbrains.io.webSocket.WebSocketServerOptions
-val CLIENT = AttributeKey.valueOf<Client>("SocketHandler.client")
+internal val CLIENT = AttributeKey.valueOf<Client>("SocketHandler.client")
class ClientManager(private val listener: ClientListener?, val exceptionHandler: ExceptionHandler, options: WebSocketServerOptions? = null) : Disposable {
- private val heartbeatTimer = SimpleTimer.getInstance().setUp(Runnable {
- synchronized (clients) {
- if (clients.isEmpty) {
- return@Runnable
- }
-
- clients.forEach { client ->
- if (client.channel.isActive) {
- client.sendHeartbeat()
- }
- true
+ private val heartbeatTimer = SimpleTimer.getInstance().setUp({
+ forEachClient(TObjectProcedure {
+ if (it.channel.isActive) {
+ it.sendHeartbeat()
}
- }
+ true
+ })
}, (options ?: WebSocketServerOptions()).heartbeatDelay.toLong())
private val clients = THashSet<Client>()
fun forEachClient(procedure: TObjectProcedure<Client>) {
synchronized (clients) {
- if (clients.isEmpty) {
- return
- }
-
clients.forEach(procedure)
}
}