client-dashboard.blade.php 17 KB

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