|
171 | 171 | } from './hug.js'
|
172 | 172 | import { Socket } from "./phoenix.esm.js"
|
173 | 173 |
|
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 | + }); |
180 | 196 | }
|
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 |
| - }); |
191 | 197 |
|
192 | 198 | // join room by key
|
193 | 199 | if (params.get("key")) {
|
|
206 | 212 | on_name_change(name)
|
207 | 213 | }
|
208 | 214 | document.querySelector("#random").onclick = () => {
|
209 |
| - socket.disconnect(); |
210 |
| - socket.connect(); |
| 215 | + reconnect(); |
211 | 216 | on_click_random();
|
212 | 217 | };
|
213 | 218 | document.querySelector("#room").onclick = () => {
|
214 |
| - socket.disconnect(); |
215 |
| - socket.connect(); |
| 219 | + reconnect(); |
216 | 220 | on_click_room();
|
217 | 221 | };
|
218 | 222 | document.querySelector("#share").onclick = () => on_click_share();
|
|
0 commit comments