Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageactionscript3
themeRDark
titleСкрипт приложения AMS republish application script
collapsetrue
var wcsServer = "192.168.0.5";
var netConnections = new Object();
var streams = new Object();
var roomName = "#amsroom1";

application.onConnect = function (client){
    trace("onConnect "+client.id);
    var nc = new NetConnection();
    nc.ping = function(){
	nc.call("pong",null);
    }
    nc.connect("rtmp://"+wcsServer+":1935/live");
    nc.onStatus = function(info){
	trace("onStatus info.code: "+info.code);
	if (info.code=="NetConnection.Connect.Success"){
	    trace("connection opened: "+wcsServer);
	}
    }
    netConnections[client.id]=nc;
    trace("onConnect done");
    return true;
}

application.onDisconnect = function (client){
    trace("onDisconnect "+client.id);
    var nc = netConnections[client.id];
    if (nc){
        nc.close();
        delete netConnections[client.id];
        trace("disconnected "+client.id);
    }
}

application.onPublish = function(client, myStream){
    var wcsStreamName = myStream.name+roomName;
    trace("onPublish "+myStream.name+" by client.id "+client.id);
    var nc = netConnections[client.id];
    var ns = new NetStream(nc);    
    ns.onStatus = function(info){
        if (info.code == "NetStream.Publish.Start"){
	    trace("now publishing "+myStream.name);
	} 
    }
    ns.attach(myStream);
    ns.publish(wcsStreamName);
    streams[myStream.name]=ns;
    trace("published stream "+wcsStreamName+" to: "+wcsServer);
    ns.publish(false);
    ns.publish(wcsStreamName);
}

application.onUnpublish = function(client, myStream){    
    trace("onUnpublish "+myStream.name+" by client.id "+client.id);
    var ns = streams[myStream.name];
    if (ns){
        ns.publish(false);
        var s = Stream.get(myStream.name);
        Stream.destroy(s);
        delete streams[myStream.name];
        trace("unpublished "+myStream.name);
    }
}

...