client-dashboard.blade.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. }
  155. if(event.stream) {
  156. var remoteViewElem = $('[data-stream="' + event.stream.id + '"]');
  157. remoteViewElem.remove();
  158. // if(remoteViewElem.length) {
  159. // remoteViewElem.attr('data-stream', '');
  160. // remoteViewElem.attr('data-connection-data', '');
  161. // remoteViewElem.attr('data-type', '');
  162. // remoteViewElem.attr('data-name', '');
  163. // }
  164. // remoteViewElem.addClass('disconnected-view')
  165. }
  166. // if no other parties in call, hang up
  167. if(!$('[data-stream]:not([data-stream="' + self.selfStreamId + '"])').length) {
  168. // self.hangUp();
  169. console.warn('No other parties in the call!');
  170. new Noty({
  171. theme: 'mint',
  172. type: 'info',
  173. text: 'All other participants have left the call',
  174. progressBar: false,
  175. timeout: 2500,
  176. }).show();
  177. self.startTime = 0;
  178. self.started = false;
  179. self.noOneElseInCall = true;
  180. }
  181. }
  182. // self disconnected
  183. self.otSession.on('sessionDisconnected', function sessionDisconnected(event) {
  184. console.log('You were disconnected from the session.', event.reason);
  185. // in case of accidental disconnection
  186. self.initOpenTok();
  187. });
  188. // initialize the publisher
  189. var publisherOptions = {
  190. videoSource: canvas.captureStream(1).getVideoTracks()[0], // TODO: Comment this line to use webcam
  191. insertMode: 'append',
  192. width: '100%',
  193. height: '100%',
  194. };
  195. self.publisher = OT.initPublisher('self-view', publisherOptions, self.handleOpenTokError_InitPublisher);
  196. self.publisher.on('streamCreated', function(event) {
  197. console.log('publisher->streamCreated');
  198. var selfView = $('#self-view');
  199. selfView.attr('data-stream', event.stream.id);
  200. selfView.attr('data-type', 'CLIENT');
  201. self.selfStreamId = event.stream.id;
  202. self.activateParty('self');
  203. $('#self-view').show();
  204. self.startTime = new Date().getTime();
  205. window.setInterval(function() {
  206. self.time = new Date().getTime() - self.startTime;
  207. }, 1000);
  208. self.started = true;
  209. });
  210. self.publisher.on('streamDestroyed', function(event) {
  211. event.preventDefault();
  212. console.log('publisher->streamDestroyed');
  213. $('#self-view').hide();
  214. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):visible');
  215. if(allThumbs.length) {
  216. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  217. if($(this).attr('data-stream') !== $('#self-view').attr('data-stream')) {
  218. self.activateParty($(this).attr('data-stream'));
  219. return false;
  220. }
  221. });
  222. }
  223. });
  224. // Connect to the session
  225. self.otSession.connect(token, function callback(error) {
  226. if (error) {
  227. self.handleOpenTokError_SessionConnect(error);
  228. } else {
  229. console.info('Connected to OT session :)');
  230. self.readyToPublish = true;
  231. }
  232. });
  233. },
  234. cameraOn: function() {
  235. if(this.readyToPublish) {
  236. this.otSession.publish(this.publisher, this.handleOpenTokError_Publish);
  237. this.publishing = true;
  238. }
  239. },
  240. cameraOff: function() {
  241. this.otSession.unpublish(this.publisher);
  242. this.publishing = false;
  243. },
  244. handleOpenTokError_SessionConnect: function(e) {
  245. console.log('handleOpenTokError_SessionConnect');
  246. console.log(e);
  247. },
  248. handleOpenTokError_Publish: function(e) {
  249. console.log('handleOpenTokError_Publish');
  250. console.log(e);
  251. },
  252. handleOpenTokError_Subscribe: function(e) {
  253. console.log('handleOpenTokError_Subscribe');
  254. console.log(e);
  255. },
  256. handleOpenTokError_InitPublisher: function(e) {
  257. console.log('handleOpenTokError_InitPublisher');
  258. console.log(e);
  259. },
  260. getClientCheckinToken: function(_done) {
  261. var self = this;
  262. $.get('/get-client-checkin-token/' + this.clientUid, function(_data) {
  263. console.log(_data);
  264. self.checkInToken = _data.data;
  265. _done();
  266. }, 'json');
  267. },
  268. getOpenTokSessionId: function(_done) {
  269. var self = this;
  270. $.ajax({
  271. type: 'post',
  272. url: '/api/clientVideoVisit/startVideoVisitAsClient',
  273. headers: {
  274. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  275. },
  276. data: {checkInToken: this.checkInToken},
  277. dataType: 'json'
  278. })
  279. .done(function (_data) {
  280. console.log(_data);
  281. if(_data.success) {
  282. self.otSessionId = _data.data;
  283. _done();
  284. }
  285. else {
  286. alert(_data.message);
  287. }
  288. })
  289. .fail(function (_data) {
  290. console.log(_data);
  291. alert(_data.message);
  292. });
  293. },
  294. activateParty: function(_stream = 'self') {
  295. var current = $('.full-view');
  296. if(current.attr('data-stream') === _stream) return;
  297. current.removeClass('full-view').addClass('thumb-view');
  298. if(current.attr('data-type') === 'CLIENT') {
  299. current.prependTo('.thumbs');
  300. }
  301. else {
  302. current.appendTo('.thumbs');
  303. }
  304. if(_stream === 'self') {
  305. $('#self-view')
  306. .removeClass('thumb-view')
  307. .removeClass('disconnected-view')
  308. .addClass('full-view')
  309. .prependTo('.main-view');
  310. }
  311. else {
  312. $('div[data-stream="' + _stream + '"]')
  313. .removeClass('thumb-view')
  314. .removeClass('disconnected-view')
  315. .addClass('full-view')
  316. .prependTo('.main-view');
  317. }
  318. }
  319. },
  320. mounted: function() {
  321. var self = this;
  322. this.getClientCheckinToken(function() { // get client check-in token
  323. self.getOpenTokSessionId(function() { // get opentok session id
  324. var name = [];
  325. @if (!empty($client->name_first)) name.push("{{ $client->name_first }}"); @endif
  326. @if (!empty($client->name_last)) name.push("{{ $client->name_last }}"); @endif
  327. self.selfName = name.join(' ');
  328. $.ajax({
  329. type: 'post',
  330. url: '/api/openTok/getClientToken',
  331. headers: {
  332. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  333. },
  334. data: {
  335. opentokSessionId: self.otSessionId,
  336. data: JSON.stringify({
  337. uid: '{{ $client->uid }}',
  338. name: self.selfName,
  339. type: 'CLIENT'
  340. })
  341. },
  342. dataType: 'json'
  343. })
  344. .done(function (_data) {
  345. console.log(_data);
  346. self.selfToken = _data.data;
  347. self.initOpenTok();
  348. })
  349. .fail(function (_data) {
  350. console.warn(_data);
  351. alert(_data.message);
  352. });
  353. });
  354. });
  355. $(document).on('click', '.thumbs>div[data-stream]', function() {
  356. self.activateParty($(this).attr('data-stream'));
  357. return false;
  358. });
  359. window.onbeforeunload = function() {
  360. if(self.started) {
  361. return "A call is in progress";
  362. }
  363. };
  364. }
  365. });
  366. </script>
  367. @endsection