Wowza Community

shared object example in AS3

Heres some code which may save you some trouble - its the shared object code from the wowza free examples in actionscript 3. Don’t forget to re-do all lables, text windows, datagrids etc completely fresh in an as3 flv.

import fl.data.DataProvider;
import fl.events.DataChangeEvent;
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0 ;
var nc:NetConnection = null;
var test_so:SharedObject = null;
function mainInit() {
	// create a connection to the wowza media server
	if (nc == null) {
		nc = new NetConnection();
		nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
		nc.connect("rtmp://***your rtmp address here***");
	}
}
// update the data for a shared object slot
function doUpdateValue(event:MouseEvent) {
	if (slotName.text != "") {
		test_so.setProperty(slotName.text,slotValue.text);
	}
}
// delete a shared object slot
function doDeleteValue(event:MouseEvent) {
	if (slotName.text != "") {
		test_so.setProperty(slotName.text,null);
		slotName.text = "";
		slotValue.text = "";
	}
}
function netStatusHandler(event:NetStatusEvent):void {
	if (event.info.code == "NetConnection.Connect.Failed") {
		trace("error");
	}
	if (event.info.code == "NetConnection.Connect.Rejected") {
		trace("error");
	} else if (event.info.code == "NetConnection.Connect.Success") {
		test_so = SharedObject.getRemote("test", nc.uri);
		test_so.addEventListener(SyncEvent.SYNC,syncEventHandler);
		test_so.connect(nc);
	} else if (event.info.code == "NetConnection.Connect.Closed") {
		trace("error");
	}
}
// fill the msg area in the ui with the result of the onSync event
function syncEventHandler(event:SyncEvent) {
	var infoObj:Object=event.changeList;
	var msg:String = "";
	msg += "onSync\n";
	for (var i = 0; i<infoObj.length; i++) {
		//infoObj contains the changed data objects (from infoObj[0] to infoobj[infoObj.length])
		//if only one slot has been changed, i=0 and no more, only infoObj[0] exists.
		//if a swf has just started and others are up and running, i will run up to the total number of slots in use for the new swf.
		//infoObj[i].name holds the slot name that was changed, infoObj[i].code holds info on the type of change/sync event.
		var info = infoObj[i];
		if (info.name != undefined) {
			msg += "   "+info.name+"="+info.code;
		} else {
			msg += "   "+info.code;
		}
		onSyncArea.text = msg;
		var dp:Array=new Array();
		dp.length = 0;
		for (var p in test_so.data) {
			dp.push({slotName: p, slotValue: test_so.data[p] });
		}
		soGrid.dataProvider=new DataProvider(dp);
		soGrid.addEventListener(Event.CHANGE,changeHandler);
		function changeHandler(e:Event):void {
			slotName.text = e.target.selectedItem.slotName;
			slotValue.text = e.target.selectedItem.slotValue;
		}
	}
}
mainInit();
doUpdate.addEventListener(MouseEvent.CLICK,doUpdateValue);
doDelete.addEventListener(MouseEvent.CLICK,doDeleteValue);

Thanks for the AS3 version of RSO example. Very helpful. This is a slightly modified version to MXML app for Flex (tested on Flex 3 beta 3)

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
<mx:Script>
	<![CDATA[
		
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0 ;
import mx.collections.*;
private var nc:NetConnection = null;
private var test_so:SharedObject = null;
private function init():void {
	// create a connection to the wowza media server
	if (nc == null) {
		nc = new NetConnection();
		nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
		nc.connect("rtmp://localhost/rso");
	}
}
private function netStatusHandler(event:NetStatusEvent):void {
	if (event.info.code == "NetConnection.Connect.Failed") {
		trace(event.info.code);
	}
	if (event.info.code == "NetConnection.Connect.Rejected") {
		trace(event.info.code);
	} else if (event.info.code == "NetConnection.Connect.Success") {
		test_so = SharedObject.getRemote("test", nc.uri);
		test_so.addEventListener(SyncEvent.SYNC,syncEventHandler);
		test_so.connect(nc);
	} else if (event.info.code == "NetConnection.Connect.Closed") {
		trace(event.info.code);
	}
}
private var dp:Array = new Array();
private function syncEventHandler(event:SyncEvent):void
{
	var infoObj:Object=event.changeList;
	var msg:String = "";
	msg += "onSyncn";
	
	for (var i:int = 0; i < infoObj.length; i++)
	 {
		//infoObj contains the changed data objects (from infoObj[0] to infoobj[infoObj.length])
		//if only one slot has been changed, i=0 and no more, only infoObj[0] exists.
		//if a swf has just started and others are up and running, i will run up to the total number of slots in use for the new swf.
		//infoObj[i].name holds the slot name that was changed, infoObj[i].code holds info on the type of change/sync event.
		var info:Object = infoObj[i];
		if (info.name != undefined) {
			msg += "   "+info.name+"="+info.code;
		} else {
			msg += "   "+info.code;
		}
		
		onSyncArea.text = msg;
		//dp=new Array();
		dp.length = 0;
		for (var p in test_so.data) {
			dp.push({slotName: p, slotValue: test_so.data[p] });
		}
		
		soGrid.dataProvider=dp;
		//soGrid.addEventListener(Event.CHANGE,changeHandler);	
	 }
}
private function changeHandler(e:Event):void {
			slotName.text = e.target.selectedItem.slotName;
			slotValue.text = e.target.selectedItem.slotValue;
		}
private function doUpdateValue(event:MouseEvent):void {
	if (slotName.text != "") {
		test_so.setProperty(slotName.text,slotValue.text);
	}
}
// delete a shared object slot
private function doDeleteValue(event:MouseEvent):void {
	if (slotName.text != "") {
		test_so.setProperty(slotName.text,null);
		slotName.text = "";
		slotValue.text = "";
	}
}			
		]]>
	</mx:Script>
	<mx:Label text="Slot"/>
	<mx:TextInput id = "slotName"/>
	<mx:Label text="Value"/>
	<mx:TextInput id = "slotValue"/>
	<mx:DataGrid id="soGrid" change="changeHandler(event)"/>
	<mx:Button id="doUpdate" click="doUpdateValue(event)" label="Update"/>
	<mx:Button id="doDelete" click="doDeleteValue(event)" label="Delete"/>
	<mx:TextArea id = "onSyncArea"/>
</mx:Application>