Skip to content

Commit

Permalink
feat: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 26, 2024
1 parent 3acd6a4 commit 8fb9f0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<title>Socket.IO Fiddle</title>
</head>
<body>
<p>State: <span id="connection-status"></span></p>
<button id="connection-toggle"></button>

<script src="/socket.io/socket.io.js"></script>
<script src="/main.js"></script>
</body>
Expand Down
22 changes: 21 additions & 1 deletion public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

(function() {

const socket = io();
const socket = io({
autoConnect: false
});

socket.on("connect", () => {
statusSpan.innerText = "Connected";
toggleBtn.innerText = "Disconnect";
console.log(`connect ${socket.id}`);
});

Expand All @@ -13,7 +17,23 @@
});

socket.on("disconnect", (reason) => {
statusSpan.innerText = "Disconnected";
toggleBtn.innerText = "Connect";
console.log(`disconnect due to ${reason}`);
});

const statusSpan = document.getElementById("connection-status");
const toggleBtn = document.getElementById("connection-toggle");

statusSpan.innerText = "Disconnected";
toggleBtn.innerText = "Connect";

toggleBtn.addEventListener("click", () => {
if (socket.connected) {
socket.disconnect();
} else {
socket.connect();
}
});

})();

0 comments on commit 8fb9f0e

Please sign in to comment.