对之前做网络电话项目的总结

 

1、js需要将swf的功能暴露给用户,这个用swfobject.js就能完成工作,引入swfobject.js后

/*
* t:需要创建的页面元素
*/
createEl = function(t, a, y, x) {
var e = document.createElement(t);
if (a) {
for (var k in a) {
if (k == 'class') {e.className = a[k];}
else if (k == 'id') {e.id = a[k];}
else {e.setAttribute(k, a[k]);e.style.cssText=a[k];}
}
}
if (y) { for (var k in y) e.style[k] = y[k]; }
if (x) { e.appendChild(document.createTextNode(x)); }
return e;
}

/*
* t:页面预加载swf
*/

 

function setupApp() {

    

var rightDiv=document.getElementById("rightDiv");

fo = new SWFObject("swf/Hwphoneapi.swf", "lzapp", "215", "138", "8");

fo.addParam("swLiveConnect", "true");

fo.addParam("name", "lzapp");

fo.addParam("id", "lzapp");

fo.addParam("wmode", "transparent");

fo.addParam("allowScriptAccess","always");

if(!rightDiv){

var major= deconcept.SWFObjectUtil.getPlayerVersion().major;

var minor= deconcept.SWFObjectUtil.getPlayerVersion().minor;

var rev= deconcept.SWFObjectUtil.getPlayerVersion().rev;
//当前flashplayer版本号
var vision=HwPhone.Device.getFlashPlayerVision();

var newElement = createEl('div',
{id: 'rightDiv', name: 'rightDiv',style: "position:absolute;top:10px;margin-left:1px;z-index:1000;float: left"});


document.body.appendChild(newElement);

var fdiv;

if(major == 0 && minor == 0 && rev == 0){

fdiv="<div id=myContent>"+"<h3>当前使用的浏览器没有安装flashplayer plugin , 请点击下面的图片进入adobe官网下载安装</h3>";

}else if(major < 8){

fdiv="<div id=myContent>"+"<h1>当前使用的浏览器的flashplayer plugin版本过低 ,请点击下面的图片进入adobe官网下载安装</h1>";

}if(major < 11){

var aa = fdiv +
"<p><a href='http://www.adobe.com/go/getflashplayer'><img src='http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a></p>"+
"</div>"

document.write(aa); 

return;

}
}

fo.write("rightDiv");

Hwphone = document.getElementById("lzapp");

rightDiv = document.getElementById("rightDiv");

Hwphone.setAttribute("width",'1px');

Hwphone.setAttribute("height",'1px');

rightDiv.style.width="1px";

rightDiv.style.height="1px";

window.setTimeout("kf_moveWithScroll()",1);

window.onerror = kf_dealErrors;

var MyMove = new Tong_MoveDiv(); 

if(!Hwphone)

alert("获取不到phone对象,swf尚未初始化完毕,请等待。")


}

/*
* t:以下是关键,js的事件的注册,监听,和触发
*/

(function(){

var version = "1.0",

mark = "HwJsMark",

topNamespace = this,

undefined,

Jx = topNamespace.Jx,

VERSIONS = {},

PACKAGES = {},

DEBUG = {

NO_DEBUG: 0,

SHOW_ALL: 1

},

option = {

debug: 1

},

out = function(msg,tag,type){

msg = String(msg);

if(option.debug){

if(this.console){

if(this.console.out){

this.console.out(msg,tag,type);

}else{

alert(msg+" "+type+" "+["+type+"]);

}

}
}

return msg;

};


try{

if(typeof Jx === "undefined" || (Jx.mark && Jx.mark === mark)){

if(Jx){

VERSIONS = Jx.VERSIONS;

PACKAGES = Jx.PACKAGES;
}
/**
* @param {Object} ver 版本号
* @param {Object} isCreateNew
*/
Jx = function(ver, isCreateNew){

var J = this;

if(isCreateNew){

this._init();

}else{

if(ver){

ver = String(ver);

try{

if(Jx.VERSIONS[ver]){

J = Jx.VERSIONS[ver];

}else{

J = Jx.VERSIONS[Jx.DEFAULT_VERSION];

throw new Error("版本号为:" + ver + ",默认版本号未: " + Jx.DEFAULT_VERSION + "!");

}

}catch(e){

J.out(e.fileName+";"+e.lineNumber+","+typeof e.stack+";"+e.name+","+e.message, 2);

}

}else{

J = Jx.VERSIONS[Jx.DEFAULT_VERSION];

}
}
return J;
};

Jx.prototype = {

version: version,

DEBUG: DEBUG,

option: option,

_init: function(){
this.constructor = Jx;
},

$namespace: function(name) {
var i,
ni,
nis = name.split("."),
ns = this;

for(i = 0; i < nis.length; i=i+1){
ni = nis[i];
ns[ni] = ns[ni] || {};
ns = ns[nis[i]];
}

return ns;
},

$package: function(){

var name = arguments[0],

func = arguments[arguments.length-1],

ns = topNamespace,

returnValue;

if(typeof func === "function"){

if(typeof name === "string"){

ns = this.$namespace(name);

if(Jx.PACKAGES[name]){

throw new Error("Package name [" + name + "] is exist!");

}else{

Jx.PACKAGES[name] = {

isLoaded: true,

returnValue: returnValue

};
}

ns.packageName = name;

}else if(typeof name === "object"){

ns = name;

}

returnValue = func.call(ns, this);

}else{

throw new Error("Function required");

}

},

checkPackage: function(name){
return Jx.PACKAGES[name];
},

out: out,


debug: function(){},
profile : function(){},
warn : function(){},
error : function(){},

startTime: +new Date(),


about: function(){
return this.out("JET (Javascript Extend Tools)\nversion: " + this.version + "\n\nCopyright (c) 2009, All rights reserved.");
},

toString: function(){
return "Huawe Javascript version " + this.version + " !";
}
};


Jx.VERSIONS = VERSIONS;

Jx.PACKAGES = PACKAGES;

Jx.VERSIONS[version] = new Jx(version, true);

Jx.DEFAULT_VERSION = version;

Jx.mark = mark;

topNamespace.Jet = topNamespace.Jx = Jx;

HwPhone = new Jx();
}else{
throw new Error("\"Jx\" name is defined in other javascript code !!!");
}
}catch(e){
out("[" + e.name + "] "+e.message+", " + e.fileName+", "+e.lineNumber+"; stack:"+typeof e.stack, 1);
}
})();

/**
* 2.[Huawei Javacript Device]
*/
Jx().$package(function(J){

var addObserver,

registrationSuccess,

registrationFailure,

connectionFailure,

callState,

callConnected,

connectionSuccess,

showSettings,

setupMic;

J.Callback = J.Callback || {};

/**
* 添加事件
* @param {Object} targetModel 事件存放的二维数组
* @param {Object} eventType 事件类型
* @param {Object} handler 用户自定义处理函数
*/
addObserver = function(targetModel, eventType, handler){

var handlers,

length,

index,

i;

if(handler){

eventType = "on" + eventType;

if(!targetModel._$events){

targetModel._$events={};

}

if(!targetModel._$events[eventType]){

targetModel._$events[eventType] = [];

}else if(targetModel._$events[eventType].length == 0){

targetModel._$events[eventType] = [];

}

handlers = targetModel._$events[eventType];

length = handlers.length;

index = -1;

for(i=0; i<length; i++){

var tempHandler = handlers[i];

if(tempHandler == handler){

index = i;

break;

}
}

if(index === -1){

handlers.push(handler);

}

}else{

J.out(">>>handler用户处理函数不能为空,请重新设置:"+targetModel+eventType+handler);

}
};
/* 通知执行事件处理
* @param argument swf传递的信息
* @param eventTypeArg 事件类型
*/
registrationSuccess = function(argument,eventTypeArg){

var handlers,

i;
var targetModel=HwPhone.Device;

var eventType;

if(!eventTypeArg){

eventType= "onReady";

}else{

eventType=eventTypeArg;

}
var flag = true;

if(targetModel._$events && targetModel._$events[eventType]){

handlers = targetModel._$events[eventType];

if(handlers.length > 0){

for(i=0; i<handlers.length; i++){

if(handlers[i].apply(targetModel, [argument]) === false){

flag = false;

}
}
}
}else{

throw new Error("事件对象存放数组: [" + targetModel + "] 事件类型: " + eventType + "未定义该相关事件类型的对象");

return false;
}

timeOutCount = 0;

return flag;

};

/*
* 继承 registrationSuccess
* 注册失败swf回调
*/
registrationFailure = function(argument){

if(timeOutCount==1){

registrationSuccess.call(this,argument,"onError");

timeOutCount=0;

}
};

/*
* 继承 registrationSuccess
* 建立连接失败swf回调
*/
connectionFailure = function(argument){

registrationSuccess.call(this,argument,"onConnectionFailure")

};

/*
* 继承 registrationSuccess
* 建立连接成功swf回调
*/
connectionSuccess = function(argument){

registrationSuccess.call(this,argument,"onConnectionSuccess")
};

/*
* 继承 registrationSuccess
* 呼叫状态swf回调
*/

callState = function(argument){

registrationSuccess.call(this,argument,"onCallState")

};

callConnected = function(argument){

registrationSuccess.call(this,argument,"onCallConnected")
};
/*
* 显示swf设置,同时注册了onShowSettings的用户,
* 以方便对页面进行细微的控制,如按钮状态的改变
*/
showSettings = function(argument){


Hwphone.focus();

Hwphone.showSettings();

registrationSuccess.call(this,argument,"onShowSettings");

};

setupMic = function(argument){

registrationSuccess.call(this,argument,"onSetupMic");

};

J.Callback.addObserver = addObserver;

J.Callback.registrationSuccess = registrationSuccess;

J.Callback.registrationFailure = registrationFailure;

J.Callback.connectionFailure = connectionFailure;

J.Callback.callState = callState;

J.Callback.callConnected = callConnected;

J.Callback.connectionSuccess = connectionSuccess;

J.Callback.showSettings = showSettings;

J.Callback.setupMic = setupMic;

});

/**
* 3.[Huawei Javacript event]
*/
Jx().$package(function(J){

var connect,

onReady,

setup,

hangup,

onError,

onConnectionFailure,

getFlashPlayerVision,

onCallState,

onCallConnected,

onConnectionSuccess,

onShowSettings,

hide,

show,

onSetupMic;

J.Device = J.Device || {};

connect = function(token,encryptedNumber){

if(Hwphone != null && Hwphone != undefined ){

HwPhone.Device.show();

Hwphone.getRtmpValue();

Hwphone.connect(token,encryptedNumber);

}

};
/*
* 显示Hwphone的swf面板
*/
show = function(){

if(Hwphone != null && Hwphone != undefined ){

Hwphone.setAttribute("width",'215px');

Hwphone.setAttribute("height",'138px');

var rightDiv2 = document.getElementById("rightDiv");

rightDiv2.style.width="215px";

rightDiv2.style.height="138px";

Hwphone.focus();
}

};
/*
* 隐藏Hwphone的swf面板
*/
hide =function(){

if(Hwphone != null && Hwphone != undefined ){

Hwphone.setAttribute("width",'1px');

Hwphone.setAttribute("height",'1px');


var rightDiv1 = document.getElementById("rightDiv");


rightDiv1.style.width="1px";

rightDiv1.style.height="1px";
}
};

//挂断
hangup = function() {

if( !! Hwphone && !! ( Hwphone.hangup )){

Hwphone.focus();

Hwphone.hangup();

}
};

//用户注册方法
setup=function(userName,passWord,fun){

timeOutCount = 1;

if( ! Hwphone || ! ( Hwphone.register )){

$.messager.alert('提示','Hwphoneapi.swf加载中,请稍后再试!','warning');

alert("213");

var buy5 = document.getElementById("buy5");

buy5.src = "img/buy2.png";

buy5.onclick = function() {

connect();

};

return;

}

if( !!Hwphone && !!(Hwphone.register) ){


HwPhone.Device.show();

interval=window.setInterval(function(){

Hwphone.register(userName,passWord);

},1000);

}

};
//注册成功事件监听
onReady = function(func) {

J.Callback.addObserver(J.Device, "Ready", func);

};
//注册失败事件监听
onError = function(func) {

J.Callback.addObserver(J.Device, "Error", func);

};
//呼叫状态事件监听
onCallState = function(func){

J.Callback.addObserver(J.Device, "CallState", func);

};

//连接失败事件监听
onConnectionFailure = function(func) {

J.Callback.addObserver(J.Device, "ConnectionFailure", func);

};

onCallConnected = function(func) {

J.Callback.addObserver(J.Device, "CallConnected", func);

};

onConnectionSuccess = function(func) {

J.Callback.addObserver(J.Device, "ConnectionSuccess", func);

};
//获得当前浏览器flashplayer plugin的版本号
getFlashPlayerVision = function(){

if(!!deconcept.SWFObjectUtil){

var visionObject=deconcept.SWFObjectUtil.getPlayerVersion();

var vision=visionObject.major+"."+visionObject.minor+"."+visionObject.rev

return vision;

}

return null;

};

onShowSettings = function(func){

J.Callback.addObserver(J.Device, "ShowSettings", func);

};
//没设置麦克风的监听注册函数
onSetupMic = function(func){

J.Callback.addObserver(J.Device, "SetupMic", func);

};

J.Device.hangup = hangup;

J.Device.setup = setup;

J.Device.onReady = onReady;

J.Device.onCallState = onCallState;

J.Device.onError = onError;

J.Device.onConnectionFailure = onConnectionFailure;

J.Device.onCallConnected = onCallConnected;

J.Device.getFlashPlayerVision = getFlashPlayerVision;

J.Device.onConnectionSuccess = onConnectionSuccess;

J.Device.onShowSettings = onShowSettings;

J.Device.connect = connect;

J.Device.hide = hide;

J.Device.show = show;

J.Device.onSetupMic = onSetupMic;

});

使用方式如下:

<script>

//注册成功

Hwphone.Device.onReady(function(msg){

   //用户自定义匿名函数,捕获触发事件及其信息

});

事件可以由swf或者后台代码来完成真正的触发:

Hwphone.Callback.registrationSuccess("注册成功");

 

</script>

表达能力有限,若有不懂得地方可以联系我。

posted @ 2012-09-06 09:52  liuminghai  阅读(1330)  评论(3编辑  收藏  举报