Skip to content
Permalink
Browse files

Better reconnect when coming back online

  • Loading branch information
darkfrog26 committed Dec 14, 2019
1 parent 3bb250d commit 8df5fc5c7e4881558822c70e409e10fd739745a9
Showing with 12 additions and 9 deletions.
  1. +11 −8 app/js/src/main/scala/io/youi/app/ClientConnectedApplication.scala
  2. +1 −1 build.sbt
@@ -6,7 +6,7 @@ import io.youi.communication.Connection
import io.youi.http.ConnectionStatus
import io.youi.net.{Protocol, URL}
import io.youi.util.Time
import org.scalajs.dom.File
import org.scalajs.dom._

import scala.concurrent.Future
import scribe.Execution.global
@@ -42,6 +42,16 @@ trait ClientConnectedApplication[C <: Connection] extends ClientApplication with
disconnected()
}
}
window.addEventListener("online", (_: Event) => {
scribe.info("Back online, reconnecting...")
connection.disconnect()
})
BackgroundUpdates.delta.attach { d =>
if (d >= 60.0) {
scribe.info(s"Resumed from sleeping, reconnecting...")
connection.disconnect()
}
}
connectCommunication match {
case ConnectCommunication.AutoConnectSynchronous => connect().map(_ => ())
case ConnectCommunication.AutoConnectAsynchronous => {
@@ -78,13 +88,6 @@ trait ClientConnectedApplication[C <: Connection] extends ClientApplication with
upload.future
}

BackgroundUpdates.delta.attach { d =>
if (d >= 60.0) {
scribe.info(s"RESUME FROM SLEEP! Delta: $d, reconnecting...")
connection.disconnect()
}
}

private def updateConnection(): Future[Unit] = {
val lastCommunication = System.currentTimeMillis() - connection.lastActive
if (connection.status() == ConnectionStatus.Open && lastCommunication > 30.seconds.toMillis) {
@@ -4,7 +4,7 @@ import sbtcrossproject.CrossType

name := "youi"
organization in ThisBuild := "io.youi"
version in ThisBuild := "0.12.10"
version in ThisBuild := "0.12.11-SNAPSHOT"
scalaVersion in ThisBuild := "2.13.1"
crossScalaVersions in ThisBuild := List("2.13.1", "2.12.10")
resolvers in ThisBuild ++= Seq(

0 comments on commit 8df5fc5

Please sign in to comment.
You can’t perform that action at this time.