Logging out of the host when closing a session

When a user closes a Reflection session window without first logging out of the host, a process may remain running on the host computer, preventing the user from logging on again. This example shows how to prevent this problem; it creates the Reflection session in a separate JavaScript window containing a script that logs the user out of the host computer when the new window is closed. In this example, the script simply checks whether the session is connected, and if it is, transmits the "logout" string.

To use this example, you must change the hostURL parameter in the <applet> tag that's part of the JavaScript function that writes out the Reflection session to a host appropriate for your network. You may also need to modify the code that transmits the "logout" string.

<html>
<head>
<title>Sample Logout on Close Script</title>
<script language="JavaScript">
<!--
    /* 
       Copy relevant symbolic constants from .js file.
    */
    var IBM3270_ENTER_KEY = 35;
    var VT_RETURN_KEY = 2;

    /*
       This function writes the applet tag to launch an IBM 3270 session in
       a new browser window called "IBM3270Applet." In addition to the 
       IBM 3270 session applet, some JavaScript functions are written to the
        tag in the new window: an "onLoad" event handler is added to
       give the terminal session the input focus when it is ready, and an
       "onUnload" event handler is added to logout of the host when the
       browser window is closed.
    */
    function launchIBMSession()
    {
        var targetwin = window.open( "", "IBM3270Applet", 
                                 "status=yes, width=650, height=550" );
        var targetdoc = targetwin.document;
        
        // Use the "with (object)" statement to simplify the following lines.
        with ( targetdoc ) {
            writeln( '<html>' );
            writeln( '<body onLoad="setSessionFocus()" onUnload="doDisconnect()">' );
            writeln( '<script language="JavaScript">' );
            writeln( '   window.onerror = errHandler;' );
            writeln( '   var api = null;' );
            writeln( '   function errHandler(msg, url, line) {' );
            writeln( '      setTimeout("setSessionFocus()",1000);' );
            writeln( '      return true;' );
            writeln( '   }' );
            writeln( '   function setSessionFocus() {' );
            writeln( '      api = document.IBM3270.getAPI("JSAPI","IBM3270");' );
            writeln( '      if ( api.getInitialized() <= 0  )' );
            writeln( '         setTimeout("setSessionFocus()",1000);' );
            writeln( '      else' );
            writeln( '         api.requestDisplayFocus();' );
            writeln( '   }' );
            writeln( '   function doDisconnect() {' );
            writeln( '      if ( api.isConnected() ) {' );
            writeln( '         api.transmitString("logout");' );
            writeln( '         api.transmitTerminalKey(" + IBM3270_ENTER_KEY + ");' );
            writeln( '      }' );
            writeln( '   }' );
            writeln( '</script>' );
            writeln( '<h1>Reflection for the Web -- IBM 3270</h1>' );
            writeln( '<applet mayscript name="IBM3270"' );
            writeln( '       codebase="./ex/"' );
            writeln( '       code="com.wrq.rweb.Launcher.class"' );
            writeln( '       width="600" height="400"' );
            writeln( '       archive="Launcher.jar">' );
            // Change the hostURL to a value appropriate for your network.
            writeln( '   <param name="hostURL" value="tn3270://payroll">' );
            writeln( '   <param name="autoconnect" value="true">' );
            writeln( '   <param name="frame" value="false">' );
            writeln( '   <param name="launcher.sessions" value="IBM3270">' );
            writeln( '</applet>' );
            writeln( '</body>' );
            writeln( '</html>' );
            close();
        }
    }

    /*
       This function writes the applet tag to launch a VT session in
       a new browser window called "VTApplet." In addition to the 
       VT session applet, some JavaScript functions are written to the
        tag in the new window: an "onLoad" event handler is added to
       give the terminal session the input focus when it is ready, and an
       "onUnload" event handler is added to logout of the host when the
       browser window is closed.
    */
    function launchVTSession()
    {
        var targetwin = window.open( "", "VTApplet", 
                                 "status=yes, width=650, height=550" );
        var targetdoc = targetwin.document;
        
        // Use the "with (object)" statement to simplify the following lines.
        with ( targetdoc ) {
            writeln( '<html>' );
            writeln( '<body onLoad="setSessionFocus()" onUnload="doDisconnect()">' );
            writeln( '<script language="JavaScript">' );
            writeln( '   window.onerror = errHandler;' );
            writeln( '   var api = null;' );
            writeln( '   function errHandler(msg, url, line) {' );
            writeln( '      setTimeout("setSessionFocus()",1000);' );
            writeln( '      return true;' );
            writeln( '   }' );
            writeln( '   function setSessionFocus() {' );
            writeln( '      api = document.VT.getAPI("JSAPI","VT");' );
            writeln( '      if ( api.getInitialized() <= 0  )' );
            writeln( '         setTimeout("setSessionFocus()",1000);' );
            writeln( '      else' );
            writeln( '         api.requestDisplayFocus();' );
            writeln( '   }' );
            writeln( '   function doDisconnect() {' );
            writeln( '      if ( api.isConnected() ) {' );
            writeln( '         api.transmitString("logout");' );
            writeln( '         api.transmitTerminalKey(" + VT_RETURN_KEY + ");' );
            writeln( '      }' );
            writeln( '   }' );
            writeln( '</script>' );
            writeln( '<h1>Reflection for the Web -- VT</h1>' );
            writeln( '<applet mayscript name="VT"' );
            writeln( '       codebase="./ex/"' );
            writeln( '       code="com.wrq.rweb.Launcher.class"' );
            writeln( '       width="600" height="400"' );
            writeln( '       archive="Launcher.jar">' );
            // Change the hostURL to a value appropriate for your network.
            writeln( '   <param name="hostURL" value="telnet://accounts">' );
            writeln( '   <param name="autoconnect" value="true">' );
            writeln( '   <param name="frame" value="false">' );
            writeln( '   <param name="launcher.sessions" value="VT">' );
            writeln( '</applet>' );
            writeln( '</body>' );
            writeln( '</html>' );
            close();
        }
    }
// -->
</script>
</head>
<body>
<h1>Reflection for the Web -- Logout on Close Example</h1>
<p>

<form name="LaunchSession">
<input type="button" name="LaunchIBM" value="Launch IBM 3270" onClick="launchIBMSession()">
<input type="button" name="LaunchVT" value="Launch VT" onClick="launchVTSession()">
</p>
</form>
</body>
</html>