Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. var global_username = '';
  2.  
  3.  
  4. /*** After successful authentication, show user interface ***/
  5.  
  6. var showUI = function () {
  7. $('div#call').show();
  8. $('form#userForm').css('display', 'none');
  9. $('div#userInfo').css('display', 'inline');
  10. $('h3#login').css('display', 'none');
  11. $('span#username').text("Hi, " + global_username);
  12. }
  13.  
  14. var greetings = {
  15. index: 0,
  16. greetings: ["Hi", "Hello"]
  17. };
  18.  
  19. setInterval(function () {
  20. greetings.index++;
  21. if (greetings.index >= greetings.greetings.length) {
  22. greetings.index = 0;
  23. }
  24. $('span#username').text(greetings.greetings[greetings.index] +
  25. ", " + global_username);
  26. }, 10000)
  27.  
  28.  
  29. /*** If no valid session could be started, show the login interface ***/
  30.  
  31. var showLoginUI = function () {
  32. $('form#userForm').css('display', 'inline');
  33. }
  34.  
  35.  
  36. //*** Set up sinchClient ***/
  37.  
  38. sinchClient = new SinchClient({
  39. applicationKey: 'c42e9183-fda2-465c-8bca-6363f7cf813c',
  40. capabilities: {
  41. calling: true
  42. },
  43.  
  44. });
  45.  
  46.  
  47. /*** Name of session, can be anything. ***/
  48.  
  49. var sessionName = 'sinchSessionPSTN-' + sinchClient.applicationKey;
  50.  
  51.  
  52. /*** Check for valid session. NOTE: Deactivated by default to allow multiple browser-tabs with different users. ***/
  53.  
  54. var sessionObj = JSON.parse(localStorage[sessionName] || '{}');
  55. if (sessionObj.userId) {
  56. sinchClient.start(sessionObj)
  57. .then(function () {
  58. global_username = sessionObj.userId;
  59. //On success, show the UI
  60. showUI();
  61. //Store session & manage in some way (optional)
  62. localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
  63. })
  64. .fail(function () {
  65. //No valid session, take suitable action, such as prompting for username/password, then start sinchClient again with login object
  66. showLoginUI();
  67. });
  68. } else {
  69. showLoginUI();
  70. }
  71.  
  72.  
  73. /*** Create user and start sinch for that user and save session in localStorage ***/
  74.  
  75. $('button#createUser').on('click', function (event) {
  76. event.preventDefault();
  77. $('button#loginUser').attr('disabled', true);
  78. $('button#createUser').attr('disabled', true);
  79. clearError();
  80.  
  81. var signUpObj = {};
  82. signUpObj.username = $('input#username').val();
  83. signUpObj.password = $('input#password').val();
  84.  
  85. //Use Sinch SDK to create a new user
  86. sinchClient.newUser(signUpObj, function (ticket) {
  87. //On success, start the client
  88. sinchClient.start(ticket, function () {
  89. global_username = signUpObj.username;
  90. //On success, show the UI
  91. showUI();
  92.  
  93. //Store session & manage in some way (optional)
  94. localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
  95. }).fail(handleError);
  96. }).fail(handleError);
  97. });
  98.  
  99.  
  100. /*** Login user and save session in localStorage ***/
  101.  
  102. $('button#loginUser').on('click', function (event) {
  103. event.preventDefault();
  104. $('button#loginUser').attr('disabled', true);
  105. $('button#createUser').attr('disabled', true);
  106. clearError();
  107.  
  108. var signInObj = {};
  109. signInObj.username = $('input#username').val();
  110. signInObj.password = $('input#password').val();
  111.  
  112. //Use Sinch SDK to authenticate a user
  113. sinchClient.start(signInObj, function () {
  114. global_username = signInObj.username;
  115. //On success, show the UI
  116. showUI();
  117.  
  118. //Store session & manage in some way (optional)
  119. localStorage[sessionName] = JSON.stringify(sinchClient.getSession());
  120. }).fail(handleError);
  121. });
  122. /*** Define listener for managing calls ***/
  123.  
  124. var callListeners = {
  125. onCallProgressing: function (call) {
  126. $('audio#ringback').prop("currentTime", 0);
  127. $('audio#ringback').trigger("play");
  128. $('div#callLog').append('<div id="stats">Ringing...</div>');
  129. },
  130. onCallEstablished: function (call) {
  131. $('audio#incoming').attr('src', call.incomingStreamURL);
  132. $('audio#ringback').trigger("pause");
  133.  
  134. //Report call stats
  135. var callDetails = call.getDetails();
  136. $('div#callLog').append('<div id="stats">Answered at: ' + (callDetails.establishedTime) + '</div>');
  137. },
  138. onCallEnded: function (call) {
  139. $('audio#ringback').trigger("pause");
  140. $('audio#incoming').attr('src', '');
  141.  
  142. $('button').removeClass('incall');
  143. $('input#phoneNumber').removeAttr('disabled');
  144.  
  145. //Report call stats
  146. var callDetails = call.getDetails();
  147. $('div#callLog').append('<div id="stats">Ended: ' + callDetails.endedTime + '</div>');
  148. $('div#callLog').append('<div id="stats">Duration (s): ' + callDetails.duration + '</div>');
  149. $('div#callLog').append('<div id="stats">End cause: ' + call.getEndCause() + '</div>');
  150. if (call.error) {
  151. $('div#callLog').append('<div id="stats">Failure message: ' + call.error.message + '</div>');
  152. }
  153. }
  154. }
  155.  
  156. /*** Make a new PSTN call ***/
  157.  
  158. var callClient = sinchClient.getCallClient();
  159. callClient.initStream().then(function () { // Directly init streams, in order to force user to accept use of media sources at a time we choose
  160. $('div.frame').not('#chromeFileWarning').show();
  161. });
  162. var call;
  163.  
  164. $('button#call').click(function (event) {
  165. event.preventDefault();
  166.  
  167. if (!$(this).hasClass("incall")) {
  168. $('button').addClass('incall');
  169. $('input#phoneNumber').attr('disabled', 'disabled');
  170.  
  171. $('div#callLog').append('<div id="title">Calling ' + $('input#phoneNumber').val() + '</div>');
  172.  
  173. call = callClient.callPhoneNumber($('input#phoneNumber').val());
  174.  
  175. call.addEventListener(callListeners);
  176. }
  177. });
  178.  
  179. $('button#hangup').click(function (event) {
  180. event.preventDefault();
  181.  
  182. if ($(this).hasClass("incall")) {
  183. call && call.hangup();
  184. }
  185. });
  186.  
  187. /*** Log out user ***/
  188.  
  189. $('button#logOut').on('click', function (event) {
  190. event.preventDefault();
  191. clearError();
  192.  
  193. //Stop the sinchClient
  194. sinchClient.terminate();
  195. //Note: sinchClient object is now considered stale. Instantiate new sinchClient to reauthenticate, or reload the page.
  196.  
  197. //Remember to destroy / unset the session info you may have stored
  198. delete localStorage[sessionName];
  199.  
  200. //Allow re-login
  201. $('button#loginUser').attr('disabled', false);
  202. $('button#createUser').attr('disabled', false);
  203.  
  204. //Reload page.
  205. window.location.reload();
  206. });
  207.  
  208.  
  209. /*** Handle errors, report them and re-enable UI ***/
  210.  
  211. var handleError = function (error) {
  212. //Enable buttons
  213. $('button#createUser').prop('disabled', false);
  214. $('button#loginUser').prop('disabled', false);
  215.  
  216. //Show error
  217. $('div.error').text(error.message);
  218. $('div.error').show();
  219. }
  220.  
  221. /** Always clear errors **/
  222. var clearError = function () {
  223. $('div.error').hide();
  224. }
  225.  
  226. /** Chrome check for file - This will warn developers of using file: protocol when testing WebRTC **/
  227. if (location.protocol == 'file:' && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
  228. $('div#chromeFileWarning').show();
  229. }
  230.  
  231. $('button').prop('disabled', false); //Solve Firefox issue, ensure buttons always clickable after load
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement