今天装了FMS,用NetConnection类试了试C与S的连接,FMS真强.
NetConnection类如下:
图片如下:
使用代码:
var t:NetConnection = new NetConnection();
t.onStatus = checkConnect;
t.connect("rtmp://localhost/test1");
function checkConnect() {
trace("连接状态="+this.isConnected);
trace("连接地址="+this.uri);
}
其中,onStatus事件处理函数的作用:当状态改变(如连接、关闭)或错误(如拒绝、失败等)时,会触发这个事件。它能显示什么信息呢?通过下列的代码可探一究竟
var t:NetConnection = new NetConnection();
t.onStatus = checkConnect;
t.connect("rtmp://localhost/test1");
function checkConnect(obj) {
for (var i in obj) {
trace(i);
}
}
输出面板中:
description
code
level
也就是说,事件对象obj有3个属性,用来描述状态,如图
图片如下:
来测试一下
var t:NetConnection = new NetConnection();
t.onStatus = checkConnect;
t.connect("rtmp://localhost/test1");
function checkConnect(obj) {
trace("代号="+obj.code)
trace("类型="+obj.level)
trace("描述="+obj.description)
}
输出:
代号=NetConnection.Connect.Success
类型=status
描述=Connection succeeded.
再测:
var t:NetConnection = new NetConnection();
t.onStatus = checkConnect;
t.connect("rtmp://localhost/test1");
//关闭服务器
t.close();
function checkConnect(obj) {
trace("代号="+obj.code);
trace("类型="+obj.level);
trace("描述="+obj.description);
}
输出:
代号=NetConnection.Connect.Closed
类型=status
描述=undefined
把服务器关掉,测试下列代码
var t:NetConnection = new NetConnection();
t.onStatus = checkConnect;
t.connect("rtmp://localhost/test1");
function checkConnect(obj) {
trace("代号="+obj.code);
trace("类型="+obj.level);
trace("描述="+obj.description);
}
输出:
代号=NetConnection.Connect.Failed
类型=error
描述=undefined
如果做实际应用,这些状态都要考虑进去
var t:NetConnection = new NetConnection();
t.onStatus = checkConnect;
t.connect("rtmp://localhost/test1");
function checkConnect(obj) {
switch (obj.code) {
case "NetConnection.Connect.Success" :
//code
break;
case "NetConnection.Connect.Failed" :
//code
break;
case "NetConnection.Connect.Closed" :
//code
break;
......
}
}
本文转自:http://www.5uflash.com/flashjiaocheng/Fmsjiaocheng/2323.html