This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A Simple Publisher/Subscriber Example
|
151
publisher at the server. A stream that is being recorded can also be played as a
recorded stream—in which case movies will receive copies of what is on disk and
will not be sent the live data as it arrives at the server. Streams can also be live-only—
in which case they are not recorded.
A Simple Publisher/Subscriber Example
Example 5-1 is a somewhat oversimplified but complete example script that shows
the essential steps in publishing one live stream and subscribing to a second live
stream. The example shows how video, audio, and ActionScript data can be sent via
a published stream and received by subscribing to another stream.
Figure 5-2. Chaining a stream between application instances running on separate FlashCom
Servers
Example 5-1. A very basic two-way messaging client
// writeln( ) replaces trace( ) and writes messages into the trace_txt field.
function writeln (msg) {
trace_txt.text += msg + "\n";
trace_txt.scroll = trace_txt.maxscroll;
}
// onStatus( ) sets up the streams and buttons when a connection is
// established or lost.
NetConnection.prototype.onStatus = function (info) {
writeln("Level: " + info.level + ", code: " + info.code);
if (info.code == "NetConnection.Connect.Success") {
out_ns = new NetStream(this);
out_ns.attachAudio(Microphone.get( ));
out_ns.attachVideo(Camera.get( ));
in_ns = new NetStream(this);
in_ns.showMessage = function (msg) {
writeln(msg);
};
// remote_video is the instance name of an embedded video object.
remote_video.attachVideo(in_ns);
in_ns.play("public/" + remoteUserName);
connect_pb.setLabel("Disconnect");
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
152
|
Chapter 5: Managing Streams
send_pb.setEnabled(true);
}
else if (!this.isConnected) {
if (out_ns) out_ns.close( );
if (in_ns) in_ns.close( );
connect_pb.setLabel("Connect");
send_pb.setEnabled(false);
}
};
NetStream.prototype.onStatus = function (info) {
for (var p in info) {
writeln("info." + p + ": " + info[p]);
}
};
// doSend( ) is called when the send_pb is clicked.
function doSend (msg) {
out_ns.send("showMessage", input_txt.text);
input_txt.text = "";
}
/* doConnect( ) is called when the connect_pb is clicked.
* It stores the text in the userName_txt and remoteUserName_txt
* fields in global variables for later use if a connection is
* attempted. See the onStatus( ) method.
*/
function doConnect (pb) {
if (pb.getLabel( ) == "Connect") {
userName = userName_txt.text;
remoteUserName = remoteUserName_txt.text;
nc.connect("rtmp:/courseChat/algebra101", userName);
}
else {
nc.close( );
}
}
function doSendStream (pb) {
if (pb.getLabel( ) == "Send Stream" && nc.isConnected) {
pb.setLabel("Stop Stream");
out_ns.publish("public/" + userName);
}
else {
pb.setLabel("Send Stream");
out_ns.publish(false);
}
}
send_pb.setEnabled(false);
nc = new NetConnection( );
Example 5-1. A very basic two-way messaging client (continued)

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.