Contents | Index | Previous | Next

Chapter 4: Broadcasting versus sending messages selectively

In addition to broadcasting mechanism, which was described in Chapter 1, Tango allows for sending data to selected members of the session.

Given the ability to obtain the names of session participants as described in the Chapter 3, we can use these names for explicit addressing of messages. Function selectiveSendJS() takes two arguments. First is an array of user names to which the message, given as the second argument, is addressed. In the example that follows, selectiveSendJS() is called when the "send" button is pressed. Note also the use of system notifications Tango_participantJoined/Left() and getParticipantsNamesJS() call for updating html forms displaying current participants of the session:

ex41.html

<html>

<body onUnload="Tango_exit()">

<script language="javascript">

function print(s)

{

Packages.java.lang.System.out.println(s);

}

function Tango_register(win)

{

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

if(Tango_agent!=null)

{

Tango_agent.setDbgModeJS(true);

Tango_agent.addTDataListenerJS(win);

Tango_agent.addTParticipantsListenerJS(win);

}

}

function Tango_exit()

{

if(Tango_agent!=null)

Tango_agent.exitJS();

}

function Tango_selectiveSend(recp,data)

{

if(Tango_agent!=null)

Tango_agent.selectiveSendJS(recp,data);

}

function Tango_receive(data)

{

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

}

function Tango_participantJoined(pNa)

{

optionsAddOption(document.forms.chat.userNames.options,pNa);

}

function Tango_participantLeft(pNa)

{

optionsRemoveOption(document.forms.chat.userNames.options,pNa);

}

function optionsToArray(opts)

{

for(var i=0,ai=0;i<opts.length;i++)

{

if(o.selected)

a[ai++]=o.value;

}

return a;

}

function optionsAddOption(opts,nam)

{

opts[opts.length]=o;

}

function optionsRemoveOption(opts,nam)

{

for(var i=0;i<opts.length;i++)

{

if(opts[i].value==nam)

{

opts[i]=null;

return;

}

}

}

</script>

<form name="chat">

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

<select name="userNames" size=4 multiple></select>

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

onclick="Tango_selectiveSend(optionsToArray(userNames.options),tty.value)">

</form>

<script language="javascript">

Tango_register(window);

if(Tango_agent!=null)

{

for(var i=0;i<part.length;i++)

optionsAddOption(document.forms.chat.userNames.options,part[i]);

}

</script>

</body>

</html>

This time we split the Java script code into two parts. The second part appears only after forms are created and takes care of their initialization. Initial content of the form "userNames" is obtained from Tango_agent.getParticipantNamesJS() call. The form is updated in Tango_participantJoined/Left() functions whenever change is reported by the Tango system (see Chapter 3 on asynchronous system notifications).

Messages are sent by Tango_agent.selectiveSendJS(), which takes array of recipients as the first parameter and the message itself as the second one. Although different method must be used for selective and nonselective sends, reception in both cases is identical: Tango_receive() is called if data listener is registered by Tango_agent.addTDataListenerJS().

Other functions used in the example are JavaScript utilities:

jsapi00090000.gif optionsToArray() re-packs array of JavaScript objects Option into array of strings;

jsapi00090000.gif optionsAdd/RemoveOption() changes elements of array of Options.