Skip to content

Commit 77a0a76

Browse files
authored
Update index.html
1 parent b88eb7a commit 77a0a76

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

.public/index.html

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,29 @@
171171
} from './hug.js'
172172
import { Socket } from "./phoenix.esm.js"
173173

174-
let socket;
175-
let params = new URLSearchParams(window.location.search);
176-
if (params.get("local")) {
177-
socket = new Socket("ws://localhost:4000/socket", { params: {} })
178-
} else {
179-
socket = new Socket("wss://hug-server.ryo12redstone.workers.dev", { params: {} })
174+
var socket;
175+
let reconnect = () => {
176+
if (socket) {
177+
socket.disconnect();
178+
}
179+
let params = new URLSearchParams(window.location.search);
180+
if (params.get("local")) {
181+
socket = new Socket("ws://localhost:4000/socket", { params: {} })
182+
} else {
183+
socket = new Socket("wss://hug-server.ryo12redstone.workers.dev", { params: {} })
184+
}
185+
socket.connect();
186+
let channel = socket.channel("player", {})
187+
channel.join()
188+
.receive("ok", resp => { console.log("Joined successfully", resp) })
189+
.receive("error", resp => { console.log("Unable to join", resp) })
190+
191+
// register event handlers for input/output.
192+
document.push = payload => channel.push("input", JSON.parse(payload));
193+
channel.on("output", payload => {
194+
on_output(JSON.stringify(payload))
195+
});
180196
}
181-
let channel = socket.channel("player", {})
182-
channel.join()
183-
.receive("ok", resp => { console.log("Joined successfully", resp) })
184-
.receive("error", resp => { console.log("Unable to join", resp) })
185-
186-
// register event handlers for input/output.
187-
document.push = payload => channel.push("input", JSON.parse(payload));
188-
channel.on("output", payload => {
189-
on_output(JSON.stringify(payload))
190-
});
191197

192198
// join room by key
193199
if (params.get("key")) {
@@ -206,13 +212,11 @@
206212
on_name_change(name)
207213
}
208214
document.querySelector("#random").onclick = () => {
209-
socket.disconnect();
210-
socket.connect();
215+
reconnect();
211216
on_click_random();
212217
};
213218
document.querySelector("#room").onclick = () => {
214-
socket.disconnect();
215-
socket.connect();
219+
reconnect();
216220
on_click_room();
217221
};
218222
document.querySelector("#share").onclick = () => on_click_share();

0 commit comments

Comments
 (0)