application.socket = new XMLSocket(); //交互用的TCP socket
application.socket.maxUnprocessedChars = 8192;//设置字符的最大长度
application.isconnect = false;
application.onAppStart = function(){
if (!application.socket.connect(IP,Port))
{
trace ("连接不到服务器:["+IP+":"+Port+"]");
}
var serverStatics = new XML();
var majorrootNode = serverStatics.createElement("ServerInfo");
serverStatics.appendChild(majorrootNode);
var rootNode = serverStatics.createElement("IntoPublish");
majorrootNode.appendChild(rootNode);
var groupNode = serverStatics.createElement("groupid");
var groupValue = serverStatics.createTextNode(application.name + "");
groupNode.appendChild(groupValue);
rootNode.appendChild(groupNode);
var streamNode = serverStatics.createElement("streamid");
var streamValue = serverStatics.createTextNode(application.stream_name + "");
streamNode.appendChild(streamValue);
rootNode.appendChild(streamNode);
application.socket.send(serverStatics); //这个要求对等端也是XMLSocket
delete serverStatics;
}
//收到服务器发过来的xml格式数据,注意(长度+1)
application.socket.onXML = function(doc)
{
trace("doc string is : ",doc.toString());
var index = 0;
var str_ip = "";
var str_port = "";
var app_name = "";
var stream_name = "";
var e = doc.firstChild;
if (e != null && e.nodeName == "Pull")
{
for(var stringNode = e.firstChild; stringNode != null;
stringNode = stringNode.nextSibling,index++)
{
if (stringNode.nodeName == "ori_ip")
{
trace("name is " + stringNode.nodeName);
trace("value is " + stringNode.firstChild.nodeValue);
str_ip = stringNode.firstChild.nodeValue;
}
else if (stringNode.nodeName == "ori_port")
{
trace("name is " + stringNode.nodeName);
trace("value is " + stringNode.firstChild.nodeValue);
str_port = stringNode.firstChild.nodeValue;
}
else if (stringNode.nodeName == "app_name")
{
trace("name is " + stringNode.nodeName);
trace("value is " + stringNode.firstChild.nodeValue);
app_name = stringNode.firstChild.nodeValue;
}
else if (stringNode.nodeName == "stream_name")
{
trace("name is " + stringNode.nodeName);
trace("value is " + stringNode.firstChild.nodeValue);
stream_name = stringNode.firstChild.nodeValue;
}
}
if ( str_ip == "" || str_port == "" || app_name == "" || stream_name == "")
{
trace("this pull stream info not ok",str_ip,str_port,app_name,stream_name);
return;
}
application.pull_connection_name = "rtmp://" + str_ip + ":" + str_port + "/" + app_name; //save the pull stream info
application.pull_groupid = app_name;
application.stream_name = stream_name;
application.stream_name_bk = stream_name;
if ( null == application.myNC )
{
application.myNC = new NetConnection();
application.myNC.onStatus = NC_onStatus;
application.myNC.onBWDone = NC_onBWDone;
}
else
{
application.myNC.close();
}
trace("pull stream is : ",application.pull_connection_name,"application.stream_name is : ",application.stream_name);
application.myNC.connect(application.pull_connection_name); //这里利用收到的远端stream info,进行拉流操作
}
}
application.socket.onConnect = function(success) { if (success) { application.isconnect = true; } }; application.socket.onClose = function() { trace("socket连接关闭!"); application.isconnect = false; };