diff options
author | Adam Chlipala <adam@chlipala.net> | 2016-06-16 11:00:01 -0400 |
---|---|---|
committer | Adam Chlipala <adam@chlipala.net> | 2016-06-16 11:00:01 -0400 |
commit | f050c7f1de1fe03e2253f408445a69e9402f60ee (patch) | |
tree | ca6e66fda36d8445e34ba396394ba5a3dcfa1a39 | |
parent | 6dad7c645d8fdb7b7237c89ff7b34e90adbb86b1 (diff) |
Client-side: detect session timeout and ask the user to reload
-rw-r--r-- | lib/js/urweb.js | 16 | ||||
-rw-r--r-- | tests/timeout.ur | 22 | ||||
-rw-r--r-- | tests/timeout.urp | 7 | ||||
-rw-r--r-- | tests/timeout.urs | 1 |
4 files changed, 44 insertions, 2 deletions
diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 96a12637..1f44a97b 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -1704,10 +1704,11 @@ function newChannel() { function listener() { var uri = path_join(url_prefix, ".msgs"); var xhr = getXHR(); - var tid, orsc, onTimeout; + var tid, orsc, onTimeout, lastTick; var connect = function () { xhr.onreadystatechange = orsc; + lastTick = new Date().getTime(); tid = window.setTimeout(onTimeout, timeout * 500); requestUri(xhr, uri, false, false); } @@ -1797,8 +1798,19 @@ function listener() { }; onTimeout = function() { + var thisTick = new Date().getTime(); xhrFinished(xhr); - connect(); + + if (thisTick - lastTick > timeout * 1000) { + if (confirm("The session for this page has expired. Please choose \"OK\" to reload.")) { + if (isPost) + history.back(); + else + location.reload(); + } + } else { + connect(); + } }; connect(); diff --git a/tests/timeout.ur b/tests/timeout.ur new file mode 100644 index 00000000..d96b42bd --- /dev/null +++ b/tests/timeout.ur @@ -0,0 +1,22 @@ +table listeners : { Ch : channel unit } + +fun ping () = + queryI1 (SELECT * FROM listeners) + (fn r => send r.Ch ()) + +fun main () = + ch <- channel; + dml (INSERT INTO listeners(Ch) VALUES ({[ch]})); + count <- source 0; + return <xml><body onload={let + fun loop () = + _ <- recv ch; + c <- get count; + set count (c + 1); + loop () + in + loop () + end}> + <dyn signal={n <- signal count; return (txt n)}/> + <button onclick={fn _ => rpc (ping ())}>Ping</button> + </body></xml> diff --git a/tests/timeout.urp b/tests/timeout.urp new file mode 100644 index 00000000..6d3ca878 --- /dev/null +++ b/tests/timeout.urp @@ -0,0 +1,7 @@ +timeout 2 +rewrite url Timeout/* +database dbname=test +sql timeout.sql +safeGet main + +timeout diff --git a/tests/timeout.urs b/tests/timeout.urs new file mode 100644 index 00000000..6ac44e0b --- /dev/null +++ b/tests/timeout.urs @@ -0,0 +1 @@ +val main : unit -> transaction page |