client-dashboard.blade.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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_last }}, {{ $client->name_first }}
  8. </span>
  9. <a href="#" class="client-logout">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. heartbeatTimer: false,
  65. },
  66. methods: {
  67. timeDisplay: function() {
  68. var seconds = this.time / 1000,
  69. minutes = parseInt(seconds / 60, 10);
  70. seconds = parseInt(seconds % 60, 10);
  71. return minutes + " min, " + seconds + " sec";
  72. },
  73. hangUp: function() {
  74. if(this.otSession) {
  75. try {
  76. this.otSession.disconnect();
  77. }
  78. catch (e) {
  79. console.log('Was already disconnected.');
  80. }
  81. this.otSession = false;
  82. this.otSessionId = '';
  83. this.started = false;
  84. this.startTime = false;
  85. // window.location = '/join';
  86. }
  87. },
  88. initOpenTok: function() {
  89. /* fake video feed (temp) */
  90. const canvas = document.createElement('canvas');
  91. canvas.width = 640;
  92. canvas.height = 480;
  93. const ctx = canvas.getContext('2d');
  94. var pos = 100;
  95. window.setInterval(function() {
  96. ctx.clearRect(0, 0, canvas.width, canvas.height);
  97. ctx.font = "20px Georgia";
  98. ctx.fillStyle = `rgb(220, 220, 220)`;
  99. ctx.fillText("Video feed from {{ $client->name_first }}", 20, pos);
  100. pos += 5;
  101. if(pos > canvas.height) pos = 100;
  102. }, 1000);
  103. var self = this;
  104. var apiKey = '<?= env('TOKBOX_API_KEY', '46678902') ?>';
  105. var sessionId = this.otSessionId;
  106. var token = this.selfToken;
  107. // destroy if existing
  108. // self.hangUp();
  109. self.otSession = OT.initSession(apiKey, sessionId);
  110. // peer connected
  111. self.otSession.on('streamCreated', function streamCreated(event) {
  112. console.log('session->streamCreated', arguments);
  113. var subscriberOptions = {
  114. insertMode: 'append',
  115. width: '100%',
  116. height: '100%'
  117. };
  118. var connectionData = JSON.parse(event.stream.connection.data);
  119. // add a div for remove view
  120. var remoteViewID = 'remote-view-' + event.stream.id;
  121. var remoteElem = $('<div id="' + remoteViewID + '" class="remote-view thumb-view" ' +
  122. 'data-stream="' + event.stream.id + '" ' +
  123. 'data-connection-data="' + event.stream.connection.data + '" ' +
  124. 'data-name="' + connectionData.name + '" ' +
  125. 'data-type="' + connectionData.type + '"></div>');
  126. remoteElem.appendTo('.thumbs');
  127. self.otSession.subscribe(event.stream, remoteViewID, subscriberOptions, self.handleOpenTokError_Subscribe);
  128. if (connectionData.type === 'PRO') {
  129. self.pro = true;
  130. }
  131. self.activateParty(event.stream.id);
  132. self.noOneElseInCall = false;
  133. });
  134. // peer disconnected
  135. self.otSession.on("streamDestroyed", function(event) {
  136. onPeerDisconnection(event, event.stream.connection.data);
  137. });
  138. // self.otSession.on("connectionDestroyed", function(event) {
  139. // debugger;
  140. // console.log('connectionDestroyed from ' + event.connection.data);
  141. // onPeerDisconnection(event, event.connection.data);
  142. // });
  143. function onPeerDisconnection(event, data) {
  144. console.log('onPeerDisconnection');
  145. if(event.stream && $('.full-view[data-stream="' + event.stream.id + '"]').length) {
  146. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):not(.disconnected-view):visible');
  147. if(allThumbs.length) {
  148. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  149. if($(this).attr('data-stream') !== event.stream.id) {
  150. self.activateParty($(this).attr('data-stream'));
  151. return false;
  152. }
  153. });
  154. }
  155. }
  156. if(event.stream) {
  157. var remoteViewElem = $('[data-stream="' + event.stream.id + '"]');
  158. remoteViewElem.remove();
  159. // if(remoteViewElem.length) {
  160. // remoteViewElem.attr('data-stream', '');
  161. // remoteViewElem.attr('data-connection-data', '');
  162. // remoteViewElem.attr('data-type', '');
  163. // remoteViewElem.attr('data-name', '');
  164. // }
  165. // remoteViewElem.addClass('disconnected-view')
  166. }
  167. // if no other parties in call, hang up
  168. if(!$('[data-stream]:not([data-stream="' + self.selfStreamId + '"])').length) {
  169. // self.hangUp();
  170. console.warn('No other parties in the call!');
  171. // new Noty({
  172. // theme: 'mint',
  173. // type: 'info',
  174. // text: 'All other participants have left the call',
  175. // progressBar: false,
  176. // timeout: 2500,
  177. // }).show();
  178. self.startTime = 0;
  179. self.started = false;
  180. self.noOneElseInCall = true;
  181. }
  182. }
  183. // self disconnected
  184. self.otSession.on('sessionDisconnected', function sessionDisconnected(event) {
  185. console.log('You were disconnected from the session.', event.reason);
  186. // turn client video off
  187. $.post('/api/clientVideoVisit/turnClientVideoOff', {}, function(_data) {
  188. console.log(_data);
  189. });
  190. // in case of accidental disconnection
  191. self.initOpenTok();
  192. });
  193. // initialize the publisher
  194. var publisherOptions = {
  195. videoSource: canvas.captureStream(1).getVideoTracks()[0], // TODO: Comment this line to use webcam
  196. insertMode: 'append',
  197. width: '100%',
  198. height: '100%',
  199. };
  200. self.publisher = OT.initPublisher('self-view', publisherOptions, self.handleOpenTokError_InitPublisher);
  201. self.publisher.on('streamCreated', function(event) {
  202. console.log('publisher->streamCreated');
  203. var selfView = $('#self-view');
  204. selfView.attr('data-stream', event.stream.id);
  205. selfView.attr('data-type', 'CLIENT');
  206. self.selfStreamId = event.stream.id;
  207. self.activateParty('self');
  208. $('#self-view').show();
  209. self.startTime = new Date().getTime();
  210. window.setInterval(function() {
  211. self.time = new Date().getTime() - self.startTime;
  212. }, 1000);
  213. self.started = true;
  214. // turn client video on
  215. $.post('/api/clientVideoVisit/turnClientVideoOn', {}, function(_data) {
  216. console.log(_data);
  217. });
  218. });
  219. self.publisher.on('streamDestroyed', function(event) {
  220. event.preventDefault();
  221. console.log('publisher->streamDestroyed');
  222. $('#self-view').hide();
  223. var allThumbs = $('.thumbs [data-stream]:not([data-stream=""]):visible');
  224. if(allThumbs.length) {
  225. $('.thumbs [data-stream]:not([data-stream=""])').each(function() {
  226. if($(this).attr('data-stream') !== $('#self-view').attr('data-stream')) {
  227. self.activateParty($(this).attr('data-stream'));
  228. return false;
  229. }
  230. });
  231. }
  232. // turn client video off
  233. $.post('/api/clientVideoVisit/turnClientVideoOff', {}, function(_data) {
  234. console.log(_data);
  235. });
  236. });
  237. // Connect to the session
  238. self.otSession.connect(token, function callback(error) {
  239. if (error) {
  240. self.handleOpenTokError_SessionConnect(error);
  241. } else {
  242. console.info('Connected to OT session :)');
  243. self.readyToPublish = true;
  244. // auto start client video
  245. self.cameraOn();
  246. }
  247. });
  248. },
  249. cameraOn: function() {
  250. if(this.readyToPublish) {
  251. this.otSession.publish(this.publisher, this.handleOpenTokError_Publish);
  252. this.publishing = true;
  253. }
  254. },
  255. cameraOff: function() {
  256. this.otSession.unpublish(this.publisher);
  257. this.publishing = false;
  258. },
  259. handleOpenTokError_SessionConnect: function(e) {
  260. console.log('handleOpenTokError_SessionConnect');
  261. console.log(e);
  262. },
  263. handleOpenTokError_Publish: function(e) {
  264. console.log('handleOpenTokError_Publish');
  265. console.log(e);
  266. },
  267. handleOpenTokError_Subscribe: function(e) {
  268. console.log('handleOpenTokError_Subscribe');
  269. console.log(e);
  270. },
  271. handleOpenTokError_InitPublisher: function(e) {
  272. console.log('handleOpenTokError_InitPublisher');
  273. console.log(e);
  274. },
  275. getClientCheckinToken: function(_done) {
  276. var self = this;
  277. $.get('/get-client-checkin-token/' + this.clientUid, function(_data) {
  278. console.log(_data);
  279. self.checkInToken = _data.data;
  280. _done();
  281. }, 'json');
  282. },
  283. getOpenTokSessionId: function(_done) {
  284. var self = this;
  285. $.ajax({
  286. type: 'post',
  287. url: '/api/clientVideoVisit/startVideoVisitAsClient',
  288. headers: {
  289. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  290. },
  291. data: {checkInToken: this.checkInToken},
  292. dataType: 'json'
  293. })
  294. .done(function (_data) {
  295. console.log(_data);
  296. if(_data.success) {
  297. self.otSessionId = _data.data;
  298. _done();
  299. }
  300. else {
  301. alert(_data.message);
  302. }
  303. })
  304. .fail(function (_data) {
  305. console.log(_data);
  306. alert(_data.message);
  307. });
  308. },
  309. activateParty: function(_stream = 'self') {
  310. var current = $('.full-view');
  311. if(current.attr('data-stream') === _stream) return;
  312. current.removeClass('full-view').addClass('thumb-view');
  313. if(current.attr('data-type') === 'CLIENT') {
  314. current.prependTo('.thumbs');
  315. }
  316. else {
  317. current.appendTo('.thumbs');
  318. }
  319. if(_stream === 'self') {
  320. $('#self-view')
  321. .removeClass('thumb-view')
  322. .removeClass('disconnected-view')
  323. .addClass('full-view')
  324. .prependTo('.main-view');
  325. }
  326. else {
  327. $('div[data-stream="' + _stream + '"]')
  328. .removeClass('thumb-view')
  329. .removeClass('disconnected-view')
  330. .addClass('full-view')
  331. .prependTo('.main-view');
  332. }
  333. }
  334. },
  335. mounted: function() {
  336. var self = this;
  337. this.getClientCheckinToken(function() { // get client check-in token
  338. self.getOpenTokSessionId(function() { // get opentok session id
  339. var name = [];
  340. @if (!empty($client->name_first)) name.push("{{ $client->name_first }}"); @endif
  341. @if (!empty($client->name_last)) name.push("{{ $client->name_last }}"); @endif
  342. self.selfName = name.join(' ');
  343. $.ajax({
  344. type: 'post',
  345. url: '/api/openTok/getClientToken',
  346. headers: {
  347. 'sessionKey': '{{ request()->cookie('sessionKey') }}'
  348. },
  349. data: {
  350. opentokSessionId: self.otSessionId,
  351. data: JSON.stringify({
  352. uid: '{{ $client->uid }}',
  353. name: self.selfName,
  354. type: 'CLIENT'
  355. })
  356. },
  357. dataType: 'json'
  358. })
  359. .done(function (_data) {
  360. console.log(_data);
  361. self.selfToken = _data.data;
  362. self.initOpenTok();
  363. })
  364. .fail(function (_data) {
  365. console.warn(_data);
  366. alert(_data.message);
  367. });
  368. });
  369. });
  370. $(document).on('click', '.thumbs>div[data-stream]', function() {
  371. self.activateParty($(this).attr('data-stream'));
  372. return false;
  373. });
  374. window.onbeforeunload = function() {
  375. if(self.started) {
  376. return "A call is in progress";
  377. }
  378. };
  379. $(document).on('click', '.client-logout', function() {
  380. // turn client video off
  381. $.post('/api/clientVideoVisit/turnClientVideoOff', {}, function(_data) {
  382. console.log(_data);
  383. // log out
  384. $.get("/api/session/logOut", function(_data) {
  385. console.log(_data);
  386. window.location = '/join';
  387. })
  388. });
  389. return false;
  390. });
  391. // start heart beat
  392. self.heartbeatTimer = window.setInterval(function() {
  393. $.post('/api/clientVideoVisit/registerClientVideoHeartbeat', {}, function(_data) {
  394. console.log(_data);
  395. });
  396. }, 5000);
  397. }
  398. });
  399. </script>
  400. @endsection