client-dashboard.blade.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php /** @var $client */ ?>
  2. @extends('layouts.meeting')
  3. @section('content')
  4. <div id="clientCallComponent">
  5. <div class="d-flex align-items-center py-3 border-bottom px-4">
  6. <span class="mr-auto">
  7. Hello {{ $client->name_first }}
  8. </span>
  9. <a href="#">Log Out</a>
  10. </div>
  11. <div class="">
  12. <div class="py-3 text-center" v-if="started">
  13. <h6 class="text-black font-weight-bold m-0">Call in progress: @{{ timeDisplay() }}</h6>
  14. </div>
  15. <div class="py-3 text-center" v-if="noOneElseInCall">
  16. <h6 class="text-black font-weight-bold m-0">A pro may pop in here at the next chance. Please wait..</h6>
  17. </div>
  18. <div class="main-view mx-auto">
  19. <div class="thumbs">
  20. <div id="self-view" class="thumb-view disconnected-view"
  21. data-name="{{ implode(" ", [$client->name_first, $client->name_last]) }}"
  22. data-type="CLIENT"></div>
  23. </div>
  24. <?php /* <button class="btn btn-danger rounded-circle hang-up"
  25. v-if="started"
  26. title="Leave Call"
  27. v-on:click.prevent="hangUp()">
  28. <i class="fa fa-phone"></i>
  29. </button> */ ?>
  30. </div>
  31. </div>
  32. <div class="d-flex justify-content-center mt-3">
  33. <button v-if="readyToPublish && !publishing"
  34. class="btn btn-sm btn-primary font-weight-bold"
  35. v-on:click.prevent="cameraOn()">Start My Camera</button>
  36. <button v-if="readyToPublish && publishing"
  37. class="btn btn-sm btn-danger font-weight-bold"
  38. v-on:click.prevent="cameraOff()">Stop My Camera</button>
  39. </div>
  40. </div>
  41. <script>
  42. window.clientCallComponent = new Vue({
  43. el: '#clientCallComponent',
  44. delimiters: ['@{{', '}}'],
  45. data: {
  46. mdPass: true, // set this to true if client has all medicare reqs met
  47. time: 0,
  48. startTime: 0,
  49. started: false,
  50. client: false,
  51. pro: false,
  52. selfName: '',
  53. selfToken: '',
  54. clientUid: '{{ $client->uid }}',
  55. checkInToken: '',
  56. otSessionId: '{{ $client->opentok_session_id }}',
  57. otSession: false,
  58. publisher: false,
  59. readyToPublish: false,
  60. publishing: false,
  61. selfUserType: 'CLIENT',
  62. selfStreamId: '',
  63. noOneElseInCall: true,
  64. },
  65. methods: {
  66. timeDisplay: function() {
  67. var seconds = this.time / 1000,
  68. minutes = parseInt(seconds / 60, 10);
  69. seconds = parseInt(seconds % 60, 10);
  70. return minutes + " min, " + seconds + " sec";
  71. },
  72. hangUp: function() {
  73. if(this.otSession) {
  74. try {
  75. this.otSession.disconnect();
  76. }
  77. catch (e) {
  78. console.log('Was already disconnected.');
  79. }
  80. this.otSession = false;
  81. this.otSessionId = '';
  82. this.started = false;
  83. this.startTime = false;
  84. // window.location = '/join';
  85. }
  86. },
  87. initOpenTok: function() {
  88. /* fake video feed (temp) */
  89. const canvas = document.createElement('canvas');
  90. canvas.width = 640;
  91. canvas.height = 480;
  92. const ctx = canvas.getContext('2d');
  93. var pos = 100;
  94. window.setInterval(function() {
  95. ctx.clearRect(0, 0, canvas.width, canvas.height);
  96. ctx.font = "20px Georgia";
  97. ctx.fillStyle = `rgb(220, 220, 220)`;
  98. ctx.fillText("Video feed from {{ $client->name_first }}", 20, pos);
  99. pos += 5;
  100. if(pos > canvas.height) pos = 100;
  101. }, 1000);
  102. var self = this;
  103. var apiKey = '<?= env('TOKBOX_API_KEY', '46678902') ?>';
  104. var sessionId = this.otSessionId;
  105. var token = this.selfToken;
  106. // destroy if existing
  107. // self.hangUp();
  108. self.otSession = OT.initSession(apiKey, sessionId);
  109. // peer connected
  110. self.otSession.on('streamCreated', function streamCreated(event) {
  111. console.log('session->streamCreated', arguments);
  112. var subscriberOptions = {
  113. insertMode: 'append',
  114. width: '100%',
  115. height: '100%'
  116. };
  117. var connectionData = JSON.parse(event.stream.connection.data);
  118. // add a div for remove view
  119. var remoteViewID = 'remote-view-' + event.stream.id;
  120. var remoteElem = $('<div id="' + remoteViewID + '" class="remote-view thumb-view" ' +
  121. 'data-stream="' + event.stream.id + '" ' +
  122. 'data-connection-data="' + event.stream.connection.data + '" ' +
  123. 'data-name="' + connectionData.name + '" ' +
  124. 'data-type="' + connectionData.type + '"></div>');
  125. remoteElem.appendTo('.thumbs');
  126. self.otSession.subscribe(event.stream, remoteViewID, subscriberOptions, self.handleOpenTokError_Subscribe);
  127. if (connectionData.type === 'PRO') {
  128. self.pro = true;
  129. }
  130. self.activateParty(event.stream.id);
  131. self.noOneElseInCall = false;
  132. });
  133. // peer disconnected
  134. self.otSession.on("streamDestroyed", function(event) {
  135. onPeerDisconnection(event, event.stream.connection.data);
  136. });
  137. // self.otSession.on("connectionDestroyed", function(event) {
  138. // debugger;
  139. // console.log('connectionDestroyed from ' + event.connection.data);
  140. // onPeerDisconnection(event, event.connection.data);
  141. // });
  142. function onPeerDisconnection(event, data) {
  143. console.log('onPeerDisconnection');
  144. if(event.stream && $('.full-view[data-stream="' + event.stream.id + '"]').length) {
  145. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):not(.disconnected-view):visible');
  146. if(allThumbs.length) {
  147. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  148. if($(this).attr('data-stream') !== event.stream.id) {
  149. self.activateParty($(this).attr('data-stream'));
  150. return false;
  151. }
  152. });
  153. }
  154. else {
  155. self.hangUp();
  156. }
  157. }
  158. if(event.stream) {
  159. var remoteViewElem = $('[data-stream="' + event.stream.id + '"]');
  160. remoteViewElem.remove();
  161. // if(remoteViewElem.length) {
  162. // remoteViewElem.attr('data-stream', '');
  163. // remoteViewElem.attr('data-connection-data', '');
  164. // remoteViewElem.attr('data-type', '');
  165. // remoteViewElem.attr('data-name', '');
  166. // }
  167. // remoteViewElem.addClass('disconnected-view')
  168. }
  169. // if no other parties in call, hang up
  170. if(!$('[data-stream]:not([data-stream="' + self.selfStreamId + '"])').length) {
  171. // self.hangUp();
  172. console.warn('No other parties in the call!');
  173. new Noty({
  174. theme: 'mint',
  175. type: 'info',
  176. text: 'All other participants have left the call',
  177. progressBar: false,
  178. timeout: false,
  179. }).show();
  180. self.startTime = 0;
  181. self.started = false;
  182. self.noOneElseInCall = true;
  183. }
  184. }
  185. // self disconnected
  186. self.otSession.on('sessionDisconnected', function sessionDisconnected(event) {
  187. console.log('You were disconnected from the session.', event.reason);
  188. });
  189. // initialize the publisher
  190. var publisherOptions = {
  191. videoSource: canvas.captureStream(1).getVideoTracks()[0], // TODO: Comment this line to use webcam
  192. insertMode: 'append',
  193. width: '100%',
  194. height: '100%',
  195. };
  196. self.publisher = OT.initPublisher('self-view', publisherOptions, self.handleOpenTokError_InitPublisher);
  197. self.publisher.on('streamCreated', function(event) {
  198. console.log('publisher->streamCreated');
  199. var selfView = $('#self-view');
  200. selfView.attr('data-stream', event.stream.id);
  201. selfView.attr('data-type', 'CLIENT');
  202. self.selfStreamId = event.stream.id;
  203. self.activateParty('self');
  204. $('#self-view').show();
  205. self.startTime = new Date().getTime();
  206. window.setInterval(function() {
  207. self.time = new Date().getTime() - self.startTime;
  208. }, 1000);
  209. self.started = true;
  210. });
  211. self.publisher.on('streamDestroyed', function(event) {
  212. event.preventDefault();
  213. console.log('publisher->streamDestroyed');
  214. $('#self-view').hide();
  215. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):visible');
  216. if(allThumbs.length) {
  217. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  218. if($(this).attr('data-stream') !== $('#self-view').attr('data-stream')) {
  219. self.activateParty($(this).attr('data-stream'));
  220. return false;
  221. }
  222. });
  223. }
  224. });
  225. // Connect to the session
  226. self.otSession.connect(token, function callback(error) {
  227. if (error) {
  228. self.handleOpenTokError_SessionConnect(error);
  229. } else {
  230. console.info('Connected to OT session :)');
  231. self.readyToPublish = true;
  232. }
  233. });
  234. },
  235. cameraOn: function() {
  236. if(this.readyToPublish) {
  237. this.otSession.publish(this.publisher, this.handleOpenTokError_Publish);
  238. this.publishing = true;
  239. }
  240. },
  241. cameraOff: function() {
  242. this.otSession.unpublish(this.publisher);
  243. this.publishing = false;
  244. },
  245. handleOpenTokError_SessionConnect: function(e) {
  246. console.log('handleOpenTokError_SessionConnect');
  247. console.log(e);
  248. },
  249. handleOpenTokError_Publish: function(e) {
  250. console.log('handleOpenTokError_Publish');
  251. console.log(e);
  252. },
  253. handleOpenTokError_Subscribe: function(e) {
  254. console.log('handleOpenTokError_Subscribe');
  255. console.log(e);
  256. },
  257. handleOpenTokError_InitPublisher: function(e) {
  258. console.log('handleOpenTokError_InitPublisher');
  259. console.log(e);
  260. },
  261. getClientCheckinToken: function(_done) {
  262. var self = this;
  263. $.get('/get-client-checkin-token/' + this.clientUid, function(_data) {
  264. console.log(_data);
  265. self.checkInToken = _data.data;
  266. _done();
  267. }, 'json');
  268. },
  269. getOpenTokSessionId: function(_done) {
  270. var self = this;
  271. $.ajax({
  272. type: 'post',
  273. url: '/api/clientVideoVisit/startVideoVisitAsClient',
  274. headers: {
  275. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  276. },
  277. data: {checkInToken: this.checkInToken},
  278. dataType: 'json'
  279. })
  280. .done(function (_data) {
  281. console.log(_data);
  282. if(_data.success) {
  283. self.otSessionId = _data.data;
  284. _done();
  285. }
  286. else {
  287. alert(_data.message);
  288. }
  289. })
  290. .fail(function (_data) {
  291. console.log(_data);
  292. alert(_data.message);
  293. });
  294. },
  295. activateParty: function(_stream = 'self') {
  296. var current = $('.full-view');
  297. if(current.attr('data-stream') === _stream) return;
  298. current.removeClass('full-view').addClass('thumb-view');
  299. if(current.attr('data-type') === 'CLIENT') {
  300. current.prependTo('.thumbs');
  301. }
  302. else {
  303. current.appendTo('.thumbs');
  304. }
  305. if(_stream === 'self') {
  306. $('#self-view')
  307. .removeClass('thumb-view')
  308. .removeClass('disconnected-view')
  309. .addClass('full-view')
  310. .prependTo('.main-view');
  311. }
  312. else {
  313. $('div[data-stream="' + _stream + '"]')
  314. .removeClass('thumb-view')
  315. .removeClass('disconnected-view')
  316. .addClass('full-view')
  317. .prependTo('.main-view');
  318. }
  319. }
  320. },
  321. mounted: function() {
  322. var self = this;
  323. this.getClientCheckinToken(function() { // get client check-in token
  324. self.getOpenTokSessionId(function() { // get opentok session id
  325. var name = [];
  326. @if (!empty($client->name_first)) name.push("{{ $client->name_first }}"); @endif
  327. @if (!empty($client->name_last)) name.push("{{ $client->name_last }}"); @endif
  328. self.selfName = name.join(' ');
  329. $.ajax({
  330. type: 'post',
  331. url: '/api/openTok/getClientToken',
  332. headers: {
  333. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  334. },
  335. data: {
  336. opentokSessionId: self.otSessionId,
  337. data: JSON.stringify({
  338. uid: '{{ $client->uid }}',
  339. name: self.selfName,
  340. type: 'CLIENT'
  341. })
  342. },
  343. dataType: 'json'
  344. })
  345. .done(function (_data) {
  346. console.log(_data);
  347. self.selfToken = _data.data;
  348. self.initOpenTok();
  349. })
  350. .fail(function (_data) {
  351. console.warn(_data);
  352. alert(_data.message);
  353. });
  354. });
  355. });
  356. $(document).on('click', '.thumbs>div[data-stream]', function() {
  357. self.activateParty($(this).attr('data-stream'));
  358. return false;
  359. });
  360. window.onbeforeunload = function() {
  361. if(self.started) {
  362. return "A call is in progress";
  363. }
  364. };
  365. }
  366. });
  367. </script>
  368. @endsection