client-dashboard.blade.php 19 KB

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