ZZDianXin_UI - 郑州电信 演示

softphone.js 7.4KB

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