var socketVoice = []; //websocket对象数组 var getTimeStringVoice = new Date().getTime().toString(); var objNew ={} //connectVoice(); //创建连接 //创建socket连接功能函数 function connectVoice(wsobj) { //浏览器支持? if("WebSocket" in window) { var host = "ws://218.29.229.185:8081/" socketVoice = new WebSocket(host); try { //连接事件 socketVoice.onopen = function(msg) { console.log(wsobj + ":连接已建立!") videoReqExten() }; //错误事件 socketVoice.onerror = function(msg) { console.log("错误:" + msg.data) } //消息事件 socketVoice.onmessage = function(msg) { console.log(wsobj+"消息接收:"+msg.data) }; //关闭事件 socketVoice.onclose = function(msg) { console.log(wsobj + ":socket closed!") }; } catch(ex) { log(ex); } } else { // 浏览器不支持 WebSocket alert("您的浏览器不支持 WebSocket!"); } } //申请注册的分机 function videoReqExten() { objNew.Type = "VideoReqExten"; objNew.AgentID = getTimeStringVoice.substring(getTimeStringVoice.length-9); objNew.AgentExten = "111"; SendVoice() } //发送 function SendVoice() { if (socketVoice.readyState != socketVoice.OPEN) { reconnect(); } if (socketVoice.readyState ==socketVoice.OPEN) { console.log(new Date() + " send " + JSON.stringify(objNew)); socketVoice.send(JSON.stringify(objNew)); objNew.Type = ""; } }