Skip to content

Commit fae061b

Browse files
committed
Fix example 5.5 and move to ECMAScript2015
1 parent c430cf7 commit fae061b

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

index.html

+50-31
Original file line numberDiff line numberDiff line change
@@ -648,53 +648,72 @@ <h3>
648648
&lt;!-- controller.html --&gt;
649649
&lt;button id="disconnectBtn" style="display: none;"&gt;Disconnect&lt;/button&gt;
650650
&lt;button id="stopBtn" style="display: none;"&gt;Stop&lt;/button&gt;
651+
&lt;button id="reconnectBtn" style="display: none;"&gt;Reconnect&lt;/button&gt;
651652
&lt;script&gt;
652-
var connection;
653+
let connection;
653654

654655
// 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 &amp;&amp; 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 &amp;&amp; connection.terminate();
662+
};
663+
664+
disconnectBtn.onclick = _ => {
665+
connection &amp;&amp; connection.close();
666+
};
667+
668+
function setConnection(newConnection) {
669+
// Disconnect from existing presentation, if not attempting to reconnect
670+
if (connection &amp;&amp; connection != newConnection && connection.state != 'closed') {
671+
connection.onclosed = undefined;
672+
connection.close();
673+
}
658674

659-
var setConnection = function (theConnection) {
660-
// Disconnect from existing presentation, if any
661-
close();
662675
// Set the new connection and save the presentation ID
663-
connection = theConnection;
676+
connection = newConnection;
664677
localStorage["presId"] = connection.id;
665678

666-
// monitor connection's state
667-
connection.onconnect = function () {
679+
function showConnectedUI() {
668680
// Allow the user to disconnect from or terminate the presentation
669-
disconnectBtn.style.display = "inline";
670681
stopBtn.style.display = "inline";
682+
disconnectBtn.style.display = "inline";
671683
reconnectBtn.style.display = "none";
684+
}
672685

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}`);
676699
};
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");
679703
};
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();
686708
};
687-
};
688709

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+
};
694716
};
695-
696-
var close = function () { connection &amp;&amp; connection.close(); };
697-
disconnectBtn.onclick = close;
698717
&lt;/script&gt;
699718
</pre>
700719
</section>

0 commit comments

Comments
 (0)