| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- var ws, n = 0,
- RandomTimeData, callNum, extenNum,
- lockReconnect = false,
- readyStateBoole,
- obj = {},
- lasttime = new Date().getTime(),
- reconnecTion, recon, loginOr = false;
- var formatDateTime = function(date) {
- var date = new Date();
- var y = date.getFullYear().toString();
- var m = date.getMonth() + 1;
- m = m < 10 ? ('0' + m) : m;
- var d = date.getDate();
- d = d < 10 ? ('0' + d) : d;
- console.log( y + m + d)
- return y + m + d
- // +' '+h+':'+minute+':'+second;
- };
- //创建scoket连接
- createWebSocket()
- function createWebSocket() {
- try {
- Connect();
- } catch(e) {
- reconnect();
- }
- }
- //连接
- //Connect();
- function Connect() {
- ws = new WebSocket("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
- ws.onopen = function() {
- lockReconnect = false
- console.log(new Date() + " " + "建立连接");
- console.log("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port)
- // extenNum = "1020"
- // sip_uri_ = "sip:"+extenNum+"@"+huayi.config.socket_ip;
- // sip_password_ = "123456"
- // ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
- // testStart();
- //建立连接时获取分机号
- lasttime = new Date().getTime();
- //申请注册分机号
- if($(".superviseBtn").text() == "我是督办专员") {
- videoReqExten()
- }
- };
- //接收到消息的回调方法
- ws.onmessage = function(evt) {
- //拿到任何消息都说明当前连接是正常的
- var myDate = new Date();
- console.log(myDate + " receive " + evt.data);
- var data = JSON.parse(evt.data)[0];
- if(data) {
- var rlt = data.Result;
- if(rlt == true) {
- var type = data.Type;
- console.log(type)
- switch(type.toLowerCase()) {
- //请求注册分机
- case "videoreqexten":
- videoreqextenBack(data);
- break; //签入
- case "videoreqideagent":
- videoreqideagentBack(data);
- break;
- case "videodesexten":
- videodesextenBack(data);
- break;
- }
- } else {
- if(data.Type = "VideoReqIdeAgent") {
- $(".registStatus").text("当前无人在线,请稍后再拨")
- $(".registStatus").css("color", "red")
- console.log("data.Result返回为false")
- }
- }
- }
- };
- //连接关闭的回调方法
- ws.onclose = function(evt) {
- //nginx proxy_read_timeout 6000s; 设置的默认关闭时间
- reconnect();
- };
- //连接发生错误的回调方法
- ws.onerror = function(evt) {
- reconnect();
- };
- }
- //重连
- function reconnect() {
- if(lockReconnect) return;
- lockReconnect = true;
- //没连接上会一直重连,设置延迟避免请求过多
- setTimeout(function() {
- console.log(new Date() + " " + "重连中……");
- createWebSocket("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
- }, 2000);
- }
- //发送
- function Send() {
- console.log("ws.readyState==" + ws.readyState)
- console.log("ws.OPEN==" + ws.OPEN)
- readyStateBoole = false
- if(ws.readyState !== ws.OPEN) {
- readyStateBoole = true
- alert("页面停留时间过长,导致分机号被占用,请刷新页面后,重新使用")
- //reconnect();
- }
- if(ws.readyState == ws.OPEN) {
- console.log(new Date() + " send " + JSON.stringify(obj));
- ws.send(JSON.stringify(obj));
- }
- }
- function videoreqextenBack(data) {
- if($(".superviseBtn").text() == "我是督办专员") {
- extenNum = data.AgentExten
- console.log(extenNum)
- sip_uri_ = "sip:" + extenNum + "@" + huayi.config.sip_uri;
- sip_password_ = "123456";
- ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
- testStart();
- } else if($(".superviseBtn").text() == "我是市民") {
- obj.Type = "VideoReqIdeAgent";
- obj.AgentID = lasttime.toString();
- obj.AgentExten = "111";
- Send();
- }
- }
- function videodesextenBack(data) {
- console.log($(".superviseBtn").text())
- }
- function videoreqideagentBack(data) {
- callNum = data.AgentExten;
- videoCall();
- }
- //申请注册分机号
- function videoReqExten() {
- obj.Type = "VideoReqExten";
- obj.AgentID = lasttime.toString();
- obj.AgentExten = "111";
- RandomTimeData = obj.AgentID;
- Send();
- }
- function extenBind() {
- var telPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
- if(!telPhone.test($("#telephone").val())) {
- $(".registStatus").text("手机号格式不正确");
- $(".registStatus").css("color", "red")
- } else {
- obj.Type = "VideoReqIdeAgent";
- obj.AgentID = lasttime.toString();
- obj.AgentExten = "111";
- Send();
- }
- }
|