Нет описания

main.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. var ws, n = 0,
  2. RandomTimeData, callNum, extenNum,
  3. lockReconnect = false,
  4. readyStateBoole,
  5. obj = {},
  6. lasttime = new Date().getTime(),
  7. reconnecTion, recon, loginOr = false;
  8. var formatDateTime = function(date) {
  9. var date = new Date();
  10. var y = date.getFullYear().toString();
  11. var m = date.getMonth() + 1;
  12. m = m < 10 ? ('0' + m) : m;
  13. var d = date.getDate();
  14. d = d < 10 ? ('0' + d) : d;
  15. console.log( y + m + d)
  16. return y + m + d
  17. // +' '+h+':'+minute+':'+second;
  18. };
  19. //创建scoket连接
  20. createWebSocket()
  21. function createWebSocket() {
  22. try {
  23. Connect();
  24. } catch(e) {
  25. reconnect();
  26. }
  27. }
  28. //连接
  29. //Connect();
  30. function Connect() {
  31. ws = new WebSocket("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  32. ws.onopen = function() {
  33. lockReconnect = false
  34. console.log(new Date() + " " + "建立连接");
  35. console.log("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port)
  36. // extenNum = "1020"
  37. // sip_uri_ = "sip:"+extenNum+"@"+huayi.config.socket_ip;
  38. // sip_password_ = "123456"
  39. // ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
  40. // testStart();
  41. //建立连接时获取分机号
  42. lasttime = new Date().getTime();
  43. //申请注册分机号
  44. if($(".superviseBtn").text() == "我是督办专员") {
  45. videoReqExten()
  46. }
  47. };
  48. //接收到消息的回调方法
  49. ws.onmessage = function(evt) {
  50. //拿到任何消息都说明当前连接是正常的
  51. var myDate = new Date();
  52. console.log(myDate + " receive " + evt.data);
  53. var data = JSON.parse(evt.data)[0];
  54. if(data) {
  55. var rlt = data.Result;
  56. if(rlt == true) {
  57. var type = data.Type;
  58. console.log(type)
  59. switch(type.toLowerCase()) {
  60. //请求注册分机
  61. case "videoreqexten":
  62. videoreqextenBack(data);
  63. break; //签入
  64. case "videoreqideagent":
  65. videoreqideagentBack(data);
  66. break;
  67. case "videodesexten":
  68. videodesextenBack(data);
  69. break;
  70. }
  71. } else {
  72. if(data.Type = "VideoReqIdeAgent") {
  73. $(".registStatus").text("当前无人在线,请稍后再拨")
  74. $(".registStatus").css("color", "red")
  75. console.log("data.Result返回为false")
  76. }
  77. }
  78. }
  79. };
  80. //连接关闭的回调方法
  81. ws.onclose = function(evt) {
  82. //nginx proxy_read_timeout 6000s; 设置的默认关闭时间
  83. reconnect();
  84. };
  85. //连接发生错误的回调方法
  86. ws.onerror = function(evt) {
  87. reconnect();
  88. };
  89. }
  90. //重连
  91. function reconnect() {
  92. if(lockReconnect) return;
  93. lockReconnect = true;
  94. //没连接上会一直重连,设置延迟避免请求过多
  95. setTimeout(function() {
  96. console.log(new Date() + " " + "重连中……");
  97. createWebSocket("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  98. }, 2000);
  99. }
  100. //发送
  101. function Send() {
  102. console.log("ws.readyState==" + ws.readyState)
  103. console.log("ws.OPEN==" + ws.OPEN)
  104. readyStateBoole = false
  105. if(ws.readyState !== ws.OPEN) {
  106. readyStateBoole = true
  107. alert("页面停留时间过长,导致分机号被占用,请刷新页面后,重新使用")
  108. //reconnect();
  109. }
  110. if(ws.readyState == ws.OPEN) {
  111. console.log(new Date() + " send " + JSON.stringify(obj));
  112. ws.send(JSON.stringify(obj));
  113. }
  114. }
  115. function videoreqextenBack(data) {
  116. if($(".superviseBtn").text() == "我是督办专员") {
  117. extenNum = data.AgentExten
  118. console.log(extenNum)
  119. sip_uri_ = "sip:" + extenNum + "@" + huayi.config.sip_uri;
  120. sip_password_ = "123456";
  121. ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
  122. testStart();
  123. } else if($(".superviseBtn").text() == "我是市民") {
  124. obj.Type = "VideoReqIdeAgent";
  125. obj.AgentID = lasttime.toString();
  126. obj.AgentExten = "111";
  127. Send();
  128. }
  129. }
  130. function videodesextenBack(data) {
  131. console.log($(".superviseBtn").text())
  132. }
  133. function videoreqideagentBack(data) {
  134. callNum = data.AgentExten;
  135. videoCall();
  136. }
  137. //申请注册分机号
  138. function videoReqExten() {
  139. obj.Type = "VideoReqExten";
  140. obj.AgentID = lasttime.toString();
  141. obj.AgentExten = "111";
  142. RandomTimeData = obj.AgentID;
  143. Send();
  144. }
  145. function extenBind() {
  146. var telPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  147. if(!telPhone.test($("#telephone").val())) {
  148. $(".registStatus").text("手机号格式不正确");
  149. $(".registStatus").css("color", "red")
  150. } else {
  151. obj.Type = "VideoReqIdeAgent";
  152. obj.AgentID = lasttime.toString();
  153. obj.AgentExten = "111";
  154. Send();
  155. }
  156. }