Brak opisu

main.js 4.4KB

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