// -*- coding: utf-8 -*- window.onload = () => { const gm = document.querySelector('#gameMaster'); const comment = document.querySelector('#gameMasterComment'); const PORT = 8080, server = location.hostname || "localhost"; let conn; const gm_error = err => { gm.innerText = '(´・ω・`)'; comment.innerText = 'ごめんね、エラーで失敗しちゃった.'; console.error(err); } const connect = () => new WebSocket(`ws://${server}:${PORT}/`); const sleep = ms => new Promise((resolve) => setTimeout(resolve, ms)); const tryReconnect = async () => { gm.innerText = `(;'ω')`; comment.innerText = '...'; initConn(); await sleep(500); if (conn.readyState !== 1) { console.log(conn.readyState); gm_error('(Error from `tryReconnect()`)'); } }; const initConn = () => { conn = connect(); conn.addEventListener("error", err => gm_error(err)); conn.addEventListener("message", e => { let {gm: gm_aa, message} = JSON.parse(e.data); gm.innerText = gm_aa; comment.innerText = message; }); conn.addEventListener("close", async e => { gm.innerText = '(´・ω・`)'; comment.innerText = `サーバとの接続が切れたみたい. ボクをクリックしてくれたら、もう一回つながるか試してみるよ.`; gm.addEventListener('click', tryReconnect, {once: true}); }); } initConn(); }