@@ -648,53 +648,72 @@ <h3>
648
648
<!-- controller.html -->
649
649
<button id="disconnectBtn" style="display: none;">Disconnect</button>
650
650
<button id="stopBtn" style="display: none;">Stop</button>
651
+ <button id="reconnectBtn" style="display: none;">Reconnect</button>
651
652
<script>
652
- var connection;
653
+ let connection;
653
654
654
655
// The Disconnect and Stop buttons are visible if there is a connected presentation
655
- var disconnectBtn = document.getElementById("disconnectBtn");
656
- var stopBtn = document.getElementById("stopBtn");
657
- stopBtn.onclick = function () { connection && connection.terminate(); };
656
+ const stopBtn = document.querySelector("#stopBtn");
657
+ const reconnectBtn = document.querySelector("#reconnectBtn");
658
+ const disconnectBtn = document.querySelector("#disconnectBtn");
659
+
660
+ stopBtn.onclick = _ => {
661
+ connection && connection.terminate();
662
+ };
663
+
664
+ disconnectBtn.onclick = _ => {
665
+ connection && connection.close();
666
+ };
667
+
668
+ function setConnection(newConnection) {
669
+ // Disconnect from existing presentation, if not attempting to reconnect
670
+ if (connection && connection != newConnection && connection.state != 'closed') {
671
+ connection.onclosed = undefined;
672
+ connection.close();
673
+ }
658
674
659
- var setConnection = function (theConnection) {
660
- // Disconnect from existing presentation, if any
661
- close();
662
675
// Set the new connection and save the presentation ID
663
- connection = theConnection ;
676
+ connection = newConnection ;
664
677
localStorage["presId"] = connection.id;
665
678
666
- // monitor connection's state
667
- connection.onconnect = function () {
679
+ function showConnectedUI() {
668
680
// Allow the user to disconnect from or terminate the presentation
669
- disconnectBtn.style.display = "inline";
670
681
stopBtn.style.display = "inline";
682
+ disconnectBtn.style.display = "inline";
671
683
reconnectBtn.style.display = "none";
684
+ }
672
685
673
- // register message handler
674
- this.onmessage = function (message) {
675
- console.log("receive message", message.data);
686
+ function showDisconnectedUI() {
687
+ disconnectBtn.style.display = "none";
688
+ stopBtn.style.display = "none";
689
+ reconnectBtn.style.display = localStorage["presId"] ? "inline" : "none";
690
+ }
691
+
692
+ // Monitor the connection state
693
+ connection.onconnect = _ => {
694
+ showConnectedUI();
695
+
696
+ // Register message handler
697
+ connection.onmessage = message => {
698
+ console.log(`Received message: ${message.data}`);
676
699
};
677
- // send initial message to presentation page
678
- this.send("say hello");
700
+
701
+ // Send initial message to presentation page
702
+ connection.send("Say hello");
679
703
};
680
- connection.onclose = reset;
681
- connection.onterminate = function () {
682
- // remove presId from localStorage if exists
683
- delete localStorage["presId"];
684
- // Reset the UI
685
- reset();
704
+
705
+ connection.onclose = _ => {
706
+ connection = null;
707
+ showDisconnectedUI();
686
708
};
687
- };
688
709
689
- var reset = function () {
690
- connection = null;
691
- disconnectBtn.style.display = "none";
692
- stopBtn.style.display = "none";
693
- reconnectBtn.style.display = localStorage["presId"] ? "inline" : "none";
710
+ connection.onterminate = _ => {
711
+ // Remove presId from localStorage if exists
712
+ delete localStorage["presId"];
713
+ connection = null;
714
+ showDisconnectedUI();
715
+ };
694
716
};
695
-
696
- var close = function () { connection && connection.close(); };
697
- disconnectBtn.onclick = close;
698
717
</script>
699
718
</ pre >
700
719
</ section >
0 commit comments