Permalink
Browse files

[feature] add Session.set_connection_lost_handler

Added a way to set a custom callback function to be called
when the client detects that it has been disconnected
from the server.

Prior to this patch, the behavior was always to print to
the jlog (browser page): "The connexion to the server seems
to have been lost." Now the programmer can customize this
behavior.

Here is a fully-functional and tested usage case:

  import stdlib.core.rpc.core;
  import stdlib.web.client;

  client function my_connection_lost_handler() {
    Client.alert("connection lost, press OK to reload");
    Client.reload();
  }

  function setup_handler() {
    Session.set_connection_lost_handler(my_connection_lost_handler);
  }

  function page() {
    <div onready={function(_) { setup_handler() }}>
     this is the <strong>custom connection handler
     test</strong> page. try killing the server and
     waiting a couple of minutes.
    </div>
  }

  Server.start(Server.http,
    {title:"connection handler test", page:page})

Caveats: like many functions in Opa, if you call
set_connection_lost_handler before the client has finished
loading the page, your app will hang. To avoid this, always
call it from within an `onready` or another type of event
handler.
  • Loading branch information...
1 parent 23f32ec commit 14754de66200d9ec03eee790266ddda91506d4dc @ogunden ogunden committed Feb 28, 2013
Showing with 42 additions and 3 deletions.
  1. +28 −3 lib/plugins/server/session.js
  2. +14 −0 lib/stdlib/core/rpc/core/session.opa
@@ -114,10 +114,18 @@ var LowLevelPingLoop = {};
var linked_with_server = true;
+ // for setting a custom handler when the connection
+ // between client and server is lost.
+ var connection_lost_handler = null;
+
function break_ping_loop() {
linked_with_server = false;
- jlog("Error: the connexion with the server seems to be lost. Please reload");
- throw("Error: the connexion with the server seems to be lost. Please reload");
+ if (connection_lost_handler == null) {
+ jlog("Error: the connection with the server seems to be lost. Please reload");
+ throw("Error: the connection with the server seems to be lost. Please reload");
+ } else {
+ connection_lost_handler();
+ }
}
// Domain used for AJAX request
@@ -469,8 +477,13 @@ var LowLevelPingLoop = {};
/** Make somethings when an error occurs with the ping loop */
function error_ping_response(xhr, str, exn){
if (failure_delay == max_failure_delay){
- jlog("Error: the connexion with the server seems to be lost");
+ if (connection_lost_handler == null) {
+ jlog("Error: the connection with the server seems to be lost.");
return;
+ } else {
+ connection_lost_handler();
+ return;
+ }
} else {
//jlog("Warning: could not reach the server. Retrying in " + failure_delay);
}
@@ -571,6 +584,10 @@ var LowLevelPingLoop = {};
domain_url = d;
}
+ LowLevelSession.set_connection_lost_handler = function(h) {
+ connection_lost_handler = h;
+ }
+
LowLevelSession.llmake = function(st, unserialize, fun_session,
ctx, dfun, more, concurrent) {
the_ping_loop();
@@ -755,6 +772,14 @@ function set_uu(x0, x1) {
}
/**
+ * @register {(-> void) -> void}
+ */
+function set_connection_lost_handler(f) {
+ LowLevelSession.set_connection_lost_handler(f);
+ return js_void;
+}
+
+/**
* @register {string -> void}
*/
function set_domain_url(d) {
@@ -190,6 +190,20 @@ Session = {{
@client set_domain_url = %%Session.set_domain_url%%
/**
+ * Set a custom handler function for the case when the
+ * connection between client and server appears to be
+ * lost. If this in not set, the default is to print
+ * "the connection with the server seems to be lost" to
+ * the client screen.
+ *
+ * Be sure to pass a `client` function here (e.g.
+ * `client function my_handler() { ... }`), as it will
+ * probably be called when the server is not available!
+ */
+ @client set_connection_lost_handler =
+ %%Session.set_connection_lost_handler%%
+
+ /**
* {2 Creating sessions}
*/

0 comments on commit 14754de

Please sign in to comment.