ZZDianXin_UI - 郑州电信 演示

softphone.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var outgoingSession = null;
  2. var incomingSession = null;
  3. var currentSession = null;
  4. var timeStrart=false;
  5. var timeReg=false;
  6. var thjsTel=false;
  7. var videoView = document.getElementById('videoView');
  8. var constraints = {
  9. audio: true,
  10. video: false,
  11. mandatory: {
  12. maxWidth: 640,
  13. maxHeight: 360
  14. }
  15. };
  16. URL = window.URL || window.webkitURL;
  17. var localStream = null;
  18. var userAgent = null;
  19. function testStart() {
  20. var sip_uri_ = huayi.config.sip_uri_.toString();
  21. var sip_password_ = huayi.config.sip_password_.toString();
  22. var ws_uri_ = huayi.config.ws_uri_.toString();
  23. console.log(sip_uri_+'-'+sip_password_+'-'+ws_uri_)
  24. console.info("get input info: sip_uri = ", sip_uri_, " sip_password = ", sip_password_, " ws_uri = ", ws_uri_);
  25. JsSIP.C.SESSION_EXPIRES = 120, JsSIP.C.MIN_SESSION_EXPIRES = 120;
  26. var socket = new JsSIP.WebSocketInterface(ws_uri_);
  27. var configuration = {
  28. sockets: [socket],
  29. outbound_proxy_set: ws_uri_,
  30. uri: sip_uri_,
  31. password: sip_password_,
  32. register: true,
  33. session_timers: true
  34. };
  35. userAgent = new JsSIP.UA(configuration);
  36. //注册成功
  37. userAgent.on('registered', function(data) {
  38. if (!timeStrart) {
  39. $(".tooltip_text").text("初始化成功...");
  40. $(".td-call").hide();
  41. }
  42. timeStrart=false;
  43. //setTimeout(function(){$(".tooltip_text").text("分机号:"+$.cookie("extno")"); }, 3000);
  44. console.info("registered: ", data.response.status_code, ",", data.response.reason_phrase);
  45. });
  46. //注册失败
  47. userAgent.on('registrationFailed', function(data) {
  48. if (!timeStrart) {
  49. $(".tooltip_text").text("注册失败...")
  50. }
  51. timeStrart=false;
  52. console.log("registrationFailed, ", data);
  53. //console.warn("registrationFailed, ", data.response.status_code, ",", data.response.reason_phrase, " cause - ", data.cause);
  54. });
  55. //注册超时
  56. userAgent.on('registrationExpiring', function() {
  57. if (!timeStrart) {
  58. $(".tooltip_text").text("注册超时...")
  59. }
  60. timeStrart=false;
  61. console.warn("registrationExpiring");
  62. });
  63. userAgent.on('newRTCSession', function(data) {
  64. console.info('onNewRTCSession: ', data);
  65. //通话呼入
  66. if(data.originator == 'remote') {
  67. // if(data.request.data.split("Extension ")[1]){
  68. // var stringHr_1=data.request.data.split("Extension ")[1].split('"')[0]
  69. // }
  70. console.info("incomingSession, answer the call----------------------");
  71. incomingSession = data.session;
  72. } else {
  73. console.info("outgoingSession");
  74. outgoingSession = data.session;
  75. outgoingSession.on('connecting', function(data) {
  76. console.info('onConnecting - ', data.request);
  77. currentSession = outgoingSession;
  78. outgoingSession = null;
  79. });
  80. }
  81. data.session.on('accepted', function(data) {
  82. console.info('onAccepted - ', data);
  83. if(data.originator == 'remote' && currentSession == null) {
  84. currentSession = incomingSession;
  85. incomingSession = null;
  86. //layer.msg("setCurrentSession - ", currentSession);
  87. }
  88. });
  89. data.session.on('confirmed', function(data) {
  90. $(".tooltip_text").text("通话中...") //呼入建立
  91. thjsTel=true;
  92. calling();
  93. // if(stringHr_1){
  94. // var p = '<a href="javascript:;" class="active J_menuTab" data-id="./callScreen/callScreen.html?Number=' + stringHr_1+'">来电弹屏<i class="fa fa-times-circle"></i></a>';
  95. // $(".J_menuTab").removeClass("active");
  96. // var nif = '<iframe class="J_iframe J_iframeNew" name="iframe" width="100%" height="100%" src="./callScreen/callScreen.html?Number=' + stringHr_1+'"></iframe>';
  97. // $(".J_mainContent").find("iframe.J_iframe").hide().parents(".J_mainContent").append(nif);
  98. // $(".J_menuTabs .page-tabs-content").append(p);
  99. // }
  100. console.info('onConfirmed - ', data);
  101. if(data.originator == 'remote' && currentSession == null) {
  102. currentSession = incomingSession;
  103. incomingSession = null;
  104. //layer.msg("setCurrentSession - ", currentSession);
  105. }
  106. });
  107. data.session.on('sdp', function(data) {
  108. console.info('onSDP, type - ', data.type, ' sdp - ', data.sdp);
  109. });
  110. data.session.on('progress', function(data) {
  111. $(".tooltip_text").text("振铃中...")
  112. thjsTel=true;
  113. //layer.msg('onProgress - ', data.originator);
  114. if(data.originator == 'remote') {
  115. //layer.msg('onProgress, response - ', data.response);
  116. }
  117. });
  118. data.session.on('peerconnection', function(data) {
  119. $(".tooltip_text").text("呼叫建立...")//呼入建立
  120. //layer.msg('onPeerconnection - ', data.peerconnection);
  121. data.peerconnection.onaddstream = function(ev) {
  122. //layer.msg('onaddstream from remote ----------- ', ev);
  123. //<!-- videoView.src = URL.createObjectURL(ev.stream); -->
  124. videoView.srcObject = ev.stream;
  125. };
  126. });
  127. });
  128. userAgent.on('newMessage', function(data) {
  129. if(data.originator == 'local') {
  130. console.info('onNewMessage , OutgoingRequest - ', data.request);
  131. } else {
  132. console.info('onNewMessage , IncomingRequest - ', data.request);
  133. }
  134. });
  135. userAgent.start();
  136. }
  137. var eventHandlers = {
  138. 'progress': function(e) {
  139. $(".tooltip_text").text("呼出振铃...");
  140. },
  141. 'failed': function(e) {
  142. $(".tooltip_text").text("呼叫失败...");
  143. //layer.msg('呼叫失败', e);
  144. },
  145. 'ended': function(e) {
  146. debugger
  147. $(".tooltip_text").text("通话结束...");
  148. clearInter();
  149. //layer.msg('呼叫结束', e); //对方挂机,通话结束
  150. },
  151. 'confirmed': function(e) {
  152. $(".tooltip_text").text("呼叫建立...");
  153. //layer.msg('呼叫建立');
  154. }
  155. };
  156. function testCall() {
  157. //呼入接听
  158. if (incomingSession) {
  159. incomingSession.answer({
  160. 'mediaConstraints': {
  161. 'audio': true,
  162. 'video': false,
  163. mandatory: {
  164. maxWidth: 640,
  165. maxHeight: 360
  166. }
  167. },
  168. 'mediaStream': localStream
  169. });
  170. incomingSession = null;
  171. }else{
  172. // 外呼拨打
  173. var sip_phone_number_ = document.getElementById("sip_phone_number").value.toString();
  174. var options = {
  175. 'eventHandlers': eventHandlers,
  176. 'mediaConstraints': {
  177. 'audio': true,
  178. 'video': false,
  179. mandatory: {
  180. maxWidth: 640,
  181. maxHeight: 360
  182. }
  183. },
  184. 'mediaStream': localStream
  185. };
  186. outgoingSession = userAgent.call(sip_phone_number_, options);
  187. var p = '<a style="background: #24c792; color: #FFFFFF;" href="javascript:;" class="active J_menuTab" data-id="./callScreen/OutCallScreen.html?Number=' + sip_phone_number_+'">外呼弹屏<i class="fa fa-times-circle"></i></a>';
  188. $(".J_menuTab").removeClass("active");
  189. var nif = '<iframe class="J_iframe J_iframeNew" name="iframe" width="100%" height="100%" src="./callScreen/OutCallScreen.html?Number=' + sip_phone_number_+'"></iframe>';
  190. $(".J_mainContent").find("iframe.J_iframe").hide().parents(".J_mainContent").append(nif);
  191. $(".J_menuTabs .page-tabs-content").append(p);
  192. }
  193. }
  194. function reg() {
  195. if (!timeReg) {
  196. $(".td-call").hide();
  197. $(".tooltip_text").text("注册成功...");
  198. }
  199. timeReg=false;
  200. console.log('register----------->');
  201. userAgent.register();
  202. }
  203. regTime();
  204. function regTime(){
  205. setInterval(function() {
  206. timeStrart=true;
  207. timeReg=true;
  208. testStart();
  209. setTimeout(function () {
  210. reg();
  211. }, 2000)
  212. }, 1000*30);
  213. }
  214. function unReg() {
  215. $(".td-call").hide();
  216. $(".tooltip_text").text("注销...");
  217. //console.log('unregister----------->');
  218. userAgent.unregister(true);
  219. }
  220. function hangup() {
  221. debugger
  222. clearInter();
  223. //window.frames[1].clearInter();
  224. $(".tooltip_text").text("挂断...");
  225. //console.log('hangup----------->');
  226. userAgent.terminateSessions();
  227. }