Contents | Index | Previous | Next

Chapter 2: Exchanging data within a session

Registered application's fundamental capability is its ability to communicate with other applications registered in the same session. The information exchange is achieved by the mean of sending and receiving messages. In case of JavaScript applications, a message takes form of a string. Sending is as simple as calling sendJS() method of Tango_agent. In order to receive, the application has to register a script page as a listener of messages. (Note the difference between registering application and registering a listener.) This is done by calling addTDataListenerJS() method of Tango_agent. Once a script is registered as a listener its method Tango_receive() will be called upon receiving messages.

We create a simple chat to illustrate sending and receiving mechanism:

chat.html

<html>

<body>

<form name="chat">

<input type="text" name="tty" size=16>

<input type="button" value="send">

</form>

</body>

</html>

Our chat is a form named "chat" made of two elements:

jsapi00090000.gif a text field named "tty"; and

jsapi00090000.gif a button with the label "send".

Now we add Tango code to it:

ex21.html

<html>

<body onUnload="Tango_exit()">

<script language="javascript">

function Tango_register(win)

{

Tango_agent=Packages.webwisdom.tango.TAgentJS.createTAgentJS(win);

}

function Tango_exit()

{

if(Tango_agent!=null)

Tango_agent.exitJS();

}

function Tango_send(m)

{

if(Tango_agent!=null)

Tango_agent.sendJS(m);

}

function Tango_receive(m)

{

document.forms.chat.tty.value=m;

}

Tango_register(window);

if(Tango_agent!=null)

Tango_agent.addTDataListenerJS(window);

</script>

<form name="chat">

<input type="text" name="tty" size=16 onclick="Tango_send(tty.value)">

<input type="button" value="send">

</form>

</body>

</html>

In the script above two functions are defined:

jsapi00090000.gif Tango_send(), which simply calls sendJS() method on Tango_agent;

jsapi00090000.gif Tango_receive(), which will be called by the Tango runtime when a message arrives. Only one Tango_receive() function can be called at a time.

The actual sending is triggered when the "send" button is pressed. The onclick event of the button is handled by Tango_send() function, invoked with the value of the text field "tty". The code registering a listener is placed after Tango_receive() function definition. This is because browsers interpret html page sequentially from top to bottom; therefore, it may happen that certain code contained on the page is executed while objects from the rest of the page are still undefined. Because registering a listener may result in immediate attempt to call Tango_receive() function we have to make sure this function is defined beforehand.