meet.blade.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. @extends('layouts.meeting')
  2. @section('content')
  3. <div id="meetComponent">
  4. {{--<h5 class="bg-dark font-weight-bold text-white m-0 py-3 px-4 d-flex">
  5. <span>Meeting</span>
  6. <span class="ml-auto" v-if="!started">
  7. Connecting...
  8. </span>
  9. <span class="ml-auto" v-if="started">
  10. <i class="fa fa-clock mr-1 text-light"></i>
  11. @{{ timeDisplay() }}
  12. </span>
  13. </h5>--}}
  14. @if(!$guest)
  15. <div class="d-flex align-items-center justify-content-center py-3 border-bottom">
  16. <span class="mr-3">
  17. {{ $pro->name_display }} | {{ $pro->is_video_visit_assistant ? 'ASSISTANT' : 'MCP' }}
  18. </span>
  19. <button class="btn btn-sm btn-primary px-4 font-weight-bold"
  20. v-on:click.prevent="nextPatient()"
  21. :disabled="client || checkingForNextPatient || started">Next Patient</button>
  22. <span v-if="patientInQueue && !started" class="patient-in-q-alert text-warning text-sm ml-2 small">
  23. <i class="fa fa-circle"></i>
  24. </span>
  25. <span v-if="!patientInQueue && !started" class="text-success text-sm ml-2 small">
  26. <i class="fa fa-circle"></i>
  27. </span>
  28. </div>
  29. <div v-if="!started && noNextPatient" class="bg-light rounded text-center py-1 font-weight-bold text-sm my-3 mx-3">@{{ noNextPatient }}</div>
  30. @endif
  31. <div class="">
  32. <div class="py-3 text-center" v-if="started">
  33. <h6 class="text-black font-weight-bold m-0">Call in progress: @{{ timeDisplay() }}</h6>
  34. </div>
  35. @if($guest)
  36. <div class="py-3 text-center" v-if="!pro || !started">
  37. <h6 class="text-black font-weight-bold m-0">Please wait. Your doctor will be with you shortly...</h6>
  38. </div>
  39. @endif
  40. <div class="main-view mx-auto" <?= !$guest ? 'v-show="!!client"' : '' ?>>
  41. <div id="self-view" class="full-view" data-type="{{ $guest ? 'CLIENT' : 'PRO' }}"></div>
  42. <div class="thumbs">
  43. <div id="remote-view-1" class="remote-view disconnected-view"
  44. data-stream="" data-from="" data-type=""></div>
  45. <div id="remote-view-2" class="remote-view disconnected-view"
  46. data-stream="" data-from="" data-type=""></div>
  47. </div>
  48. <button class="btn btn-danger rounded-circle hang-up"
  49. v-if="started"
  50. title="Leave Call"
  51. v-on:click.prevent="hangUp()">
  52. <i class="fa fa-phone"></i>
  53. </button>
  54. <button class="btn btn-success rounded-circle call-mcp"
  55. v-if="selfUserType === 'ASSISTANT'"
  56. title="Call MCP Pro"
  57. v-on:click.prevent="callMCPPro()">
  58. <i class="fa fa-user-md"></i>
  59. </button>
  60. </div>
  61. </div>
  62. </div>
  63. <script>
  64. new Vue({
  65. el: '#meetComponent',
  66. delimiters: ['@{{', '}}'],
  67. data: {
  68. time: 0,
  69. startTime: 0,
  70. started: false,
  71. client: false,
  72. pro: false,
  73. selfName: '',
  74. selfToken: '',
  75. @if($guest)
  76. clientUid: '',
  77. checkInToken: '',
  78. @endif
  79. otSessionId: '',
  80. @if(!$guest)
  81. checkingForNextPatient: false,
  82. noNextPatient: false,
  83. @endif
  84. otSession: false,
  85. selfUserType: false,
  86. patientInQueue: false,
  87. },
  88. methods: {
  89. @if(!$guest)
  90. pollForNextPatient: function() {
  91. if(!this.started) {
  92. this.nextPatient(true);
  93. }
  94. },
  95. nextPatient: function(_pollOnly = false) {
  96. var self = this;
  97. if(!_pollOnly) this.checkingForNextPatient = true;
  98. $.post('/api/client/getNextClientForVideoVisit', {}, function(_data) {
  99. if(_pollOnly) {
  100. self.patientInQueue = _data.success;
  101. }
  102. else {
  103. self.checkingForNextPatient = false;
  104. if(!_data.success) {
  105. self.noNextPatient = _data.message;
  106. window.setTimeout(function() {
  107. self.noNextPatient = false;
  108. }, 2000);
  109. }
  110. else {
  111. // get ot session key from client record
  112. self.client = true;
  113. self.clientUid = _data.data;
  114. self.getOpenTokSessionId(function() {
  115. self.selfName = '{{ $pro->name_display }}';
  116. $.post('/api/openTok/getClientToken', {
  117. opentokSessionId: self.otSessionId,
  118. name: self.selfName
  119. }, function (_data) {
  120. self.selfToken = _data.data;
  121. self.initOpenTok();
  122. });
  123. });
  124. }
  125. }
  126. }, 'json');
  127. },
  128. @endif
  129. getInitials: function(_name) {
  130. var parts = _name.split(/\s+/g);
  131. parts = parts.map(_part => _part[0]);
  132. return parts.join('');
  133. },
  134. timeDisplay: function() {
  135. var seconds = this.time / 1000,
  136. minutes = parseInt(seconds / 60, 10);
  137. seconds = parseInt(seconds % 60, 10);
  138. return minutes + " min, " + seconds + " sec";
  139. },
  140. hangUp: function() {
  141. if(this.otSession) {
  142. this.otSession.disconnect();
  143. this.otSession = false;
  144. this.otSessionId = '';
  145. this.started = false;
  146. this.startTime = false;
  147. @if(!$guest)
  148. this.client = false;
  149. @else
  150. window.location = '/join';
  151. @endif
  152. }
  153. },
  154. @if(!$guest)
  155. callMCPPro: function() {
  156. // put client in mcp queue
  157. $.ajax({
  158. type: 'post',
  159. url: '/api/clientVideoVisit/PutVideoVisitInMcpQueue',
  160. headers: {
  161. 'sessionKey': localStorage.sessionKey
  162. },
  163. data: {uid: this.clientUid},
  164. dataType: 'json'
  165. })
  166. .done(function (_data) {
  167. console.log(_data);
  168. if(_data.success) {
  169. new Noty({
  170. theme: 'mint',
  171. type: 'success',
  172. text: 'Client added to MCP call queue',
  173. progressBar: false,
  174. timeout: 1500,
  175. }).show();
  176. }
  177. else {
  178. new Noty({
  179. theme: 'mint',
  180. type: 'alert',
  181. text: _data.message,
  182. progressBar: false,
  183. timeout: 3000,
  184. }).show();
  185. }
  186. })
  187. .fail(function (_data) {
  188. console.log(_data);
  189. // alert(_data.message);
  190. });
  191. },
  192. @endif
  193. // OT methods
  194. initOpenTok: function() {
  195. /* fake video feed (temp) */
  196. const randomColour = () => {
  197. return Math.round(Math.random() * 255);
  198. };
  199. const canvas = document.createElement('canvas');
  200. canvas.width = 640;
  201. canvas.height = 480;
  202. const ctx = canvas.getContext('2d');
  203. var pos = 100;
  204. window.setInterval(function() {
  205. ctx.clearRect(0, 0, canvas.width, canvas.height);
  206. ctx.font = "20px Georgia";
  207. ctx.fillStyle = `rgb(220, 220, 220)`;
  208. var userType = '<?= $guest? "Client" : "Pro" ?>';
  209. ctx.fillText("Video feed from the " + userType, 20, pos);
  210. pos += 5;
  211. if(pos > canvas.height) pos = 100;
  212. }, 1000);
  213. var self = this;
  214. var apiKey = '<?= env('TOKBOX_API_KEY', '46678902') ?>';
  215. var sessionId = this.otSessionId;
  216. var token = this.selfToken;
  217. // destroy if existing
  218. self.hangUp();
  219. self.otSession = OT.initSession(apiKey, sessionId);
  220. // peer connected
  221. self.otSession.on('streamCreated', function streamCreated(event) {
  222. var subscriberOptions = {
  223. insertMode: 'append',
  224. width: '100%',
  225. height: '100%'
  226. };
  227. var remoteViewElem = 'remote-view-1';
  228. if($('#remote-view-1').attr('data-stream')) {
  229. remoteViewElem = 'remote-view-2';
  230. }
  231. self.otSession.subscribe(event.stream, remoteViewElem, subscriberOptions, self.handleOpenTokError);
  232. remoteViewElem = $('#' + remoteViewElem);
  233. remoteViewElem.attr('data-stream', event.stream.id);
  234. remoteViewElem.attr('data-from', event.stream.connection.data.split('|')[0]);
  235. var userType = event.target.connection.data.split('|')[1];
  236. if(userType === 'CLIENT') {
  237. remoteViewElem.attr('data-type', 'CLIENT');
  238. }
  239. else {
  240. remoteViewElem.attr('data-type', 'PRO');
  241. }
  242. @if($guest)
  243. self.pro = true;
  244. @else
  245. self.joinMeetingAsPro();
  246. self.client = true;
  247. @endif
  248. if(!self.startTime) {
  249. self.startTime = new Date().getTime();
  250. window.setInterval(function() {
  251. self.time = new Date().getTime() - self.startTime;
  252. }, 1000);
  253. self.started = true;
  254. }
  255. self.activateParty(event.stream.id);
  256. });
  257. // peer disconnected
  258. self.otSession.on("streamDestroyed", function(event) {
  259. self.activateParty('self');
  260. var remoteViewElem = $('[data-stream="' + event.stream.id + '"]');
  261. if(remoteViewElem.length) {
  262. remoteViewElem.attr('data-stream', '');
  263. remoteViewElem.attr('data-from', '');
  264. }
  265. @if($guest)
  266. self.pro = false;
  267. @else
  268. self.client = false;
  269. @endif
  270. // if no other parties in call, hang up
  271. if(!$('[data-stream]:not([data-stream=""])').length) {
  272. self.hangUp();
  273. }
  274. });
  275. // self connected
  276. self.otSession.on("sessionConnected", function(event) {
  277. console.log(event);
  278. // call join as assistant/pro here
  279. self.selfUserType = event.target.connection.data.split('|')[1];
  280. });
  281. // self disconnected
  282. self.otSession.on('sessionDisconnected', function sessionDisconnected(event) {
  283. console.log('You were disconnected from the session.', event.reason);
  284. });
  285. // initialize the publisher
  286. var publisherOptions = {
  287. videoSource: canvas.captureStream(1).getVideoTracks()[0],
  288. insertMode: 'append',
  289. width: '100%',
  290. height: '100%',
  291. };
  292. var publisher = OT.initPublisher('self-view', publisherOptions, self.handleOpenTokError);
  293. publisher.on('streamCreated', function(event) {
  294. var selfView = $('#self-view');
  295. selfView.attr('data-stream', event.stream.id);
  296. selfView.attr('data-from', event.stream.connection.data.split('|')[0]);
  297. @if($guest)
  298. selfView.attr('data-type', 'CLIENT');
  299. @else
  300. selfView.attr('data-type', 'PRO');
  301. @endif
  302. });
  303. // Connect to the session
  304. self.otSession.connect(token, function callback(error) {
  305. if (error) {
  306. self.handleOpenTokError(error);
  307. } else {
  308. // If the connection is successful, publish the publisher to the session
  309. self.otSession.publish(publisher, self.handleOpenTokError);
  310. }
  311. });
  312. },
  313. handleOpenTokError: function(e) {
  314. },
  315. getClientCheckinToken: function(_done) {
  316. var self = this;
  317. $.get('/get-client-checkin-token/' + this.clientUid, function(_data) {
  318. console.log(_data);
  319. self.checkInToken = _data.data;
  320. _done();
  321. }, 'json');
  322. },
  323. getOpenTokSessionId: function(_done) {
  324. var self = this;
  325. @if($guest)
  326. $.ajax({
  327. type: 'post',
  328. url: '/api/clientVideoVisit/startVideoVisitAsClient',
  329. headers: {
  330. 'sessionKey': localStorage.sessionKey
  331. },
  332. data: {checkInToken: this.checkInToken},
  333. dataType: 'json'
  334. })
  335. .done(function (_data) {
  336. console.log(_data);
  337. if(_data.success) {
  338. self.otSessionId = _data.data;
  339. _done();
  340. }
  341. else {
  342. alert(_data.message);
  343. }
  344. })
  345. .fail(function (_data) {
  346. console.log(_data);
  347. alert(_data.message);
  348. });
  349. @else
  350. $.get('/pro/get-opentok-session-key/' + self.clientUid, function(_data) {
  351. self.otSessionId = _data.data;
  352. _done();
  353. }, 'json');
  354. @endif
  355. },
  356. @if(!$guest)
  357. joinMeetingAsPro: function() {
  358. var self = this;
  359. $.ajax({
  360. type: 'post',
  361. url: '/api/clientVideoVisit/joinVideoVisitAsMcpPro',
  362. headers: {
  363. 'sessionKey': localStorage.sessionKey
  364. },
  365. data: {uid: self.clientUid},
  366. dataType: 'json'
  367. })
  368. .done(function (_data) {
  369. console.log(_data);
  370. })
  371. .fail(function (_data) {
  372. console.warn(_data);
  373. alert(_data.message);
  374. });
  375. },
  376. @endif
  377. activateParty: function(_stream = 'self') {
  378. var current = $('.full-view');
  379. if(current.attr('data-stream') === _stream) return;
  380. current.removeClass('full-view').addClass('thumb-view');
  381. if(current.attr('data-type') === 'CLIENT') {
  382. current.prependTo('.thumbs');
  383. }
  384. else {
  385. current.appendTo('.thumbs');
  386. }
  387. if(_stream === 'self') {
  388. $('#self-view')
  389. .removeClass('thumb-view')
  390. .removeClass('disconnected-view')
  391. .addClass('full-view')
  392. .prependTo('.main-view');
  393. }
  394. else {
  395. $('div[data-stream="' + _stream + '"]')
  396. .removeClass('thumb-view')
  397. .removeClass('disconnected-view')
  398. .addClass('full-view')
  399. .prependTo('.main-view');
  400. }
  401. }
  402. },
  403. mounted: function() {
  404. var self = this;
  405. @if($guest)
  406. this.clientUid = localStorage.clientUid;
  407. this.getClientCheckinToken(function() { // get client check-in token
  408. self.getOpenTokSessionId(function() { // get opentok session id
  409. var name = [];
  410. if (localStorage.clientFirstName) name.push(localStorage.clientFirstName);
  411. if (localStorage.clientLastName) name.push(localStorage.clientLastName);
  412. this.selfName = name.join(' ');
  413. $.ajax({
  414. type: 'post',
  415. url: '/api/openTok/getClientToken',
  416. headers: {
  417. 'sessionKey': localStorage.sessionKey
  418. },
  419. data: {
  420. opentokSessionId: self.otSessionId,
  421. name: name.join(' ')
  422. },
  423. dataType: 'json'
  424. })
  425. .done(function (_data) {
  426. console.log(_data);
  427. self.selfToken = _data.data;
  428. self.initOpenTok();
  429. })
  430. .fail(function (_data) {
  431. console.warn(_data);
  432. alert(_data.message);
  433. });
  434. });
  435. });
  436. @else
  437. localStorage.sessionKey = '{{ $session->session_key }}';
  438. @endif
  439. $(document).on('click', '.thumbs>div[data-stream]', function() {
  440. self.activateParty($(this).attr('data-stream'));
  441. return false;
  442. });
  443. @if(!$guest)
  444. // poll for new patients and alert
  445. window.setInterval(function() {
  446. self.pollForNextPatient();
  447. }, 5000);
  448. @endif
  449. }
  450. });
  451. </script>
  452. @endsection