[feature] add Session.set_connection_lost_handler #108

Merged
merged 1 commit into from Mar 5, 2013

Projects

None yet

2 participants

@ogunden
Contributor
ogunden commented Feb 28, 2013

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.

@ogunden ogunden [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.
14754de
@BourgerieQuentin BourgerieQuentin added a commit that referenced this pull request Mar 5, 2013
@BourgerieQuentin BourgerieQuentin [refactor] runtime, ping: Just a quick refactor of the pull request #108
, no more set_connection_lost_handler but add_connection_lost_handler and move to PingClient
4e48661
@BourgerieQuentin BourgerieQuentin merged commit 14754de into MLstate:master Mar 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment