diff --git a/index.html b/index.html index de03d8c..6749c0d 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@ +

WS:

UserID:


diff --git a/script.js b/script.js index b9388b4..0abdaa2 100644 --- a/script.js +++ b/script.js @@ -1,3 +1,8 @@ +let ws = null + +let wsAddress = "ws://localhost:" +let wsPort = 3000 + window.onload = () => { const msg = document.getElementById("msg") const receiver = document.getElementById("receiver") @@ -23,16 +28,29 @@ window.onload = () => { document.getElementById("receiver").value = "" } + document.getElementById("change-ws").onclick = () => { + if (wsPort === 3000) { + wsPort = 3001 + } else { + wsPort = 3000 + } + + if (ws.readyState == 1) { + ws.close() + } + } + connectWS() } -let ws = null + function connectWS() { - ws = new WebSocket("ws://localhost:3000/") + ws = new WebSocket(wsAddress + wsPort) ws.onopen = () => { console.info("ws open", document.getElementById("userid")) + document.getElementById("ws").innerHTML = ws.url } ws.onmessage = (msg) => { @@ -46,7 +64,9 @@ function connectWS() { } ws.onclose = (e) => { - console.log("ws closed", e.reason) + if (e.reason.code === 1005) return + + console.log("ws closed", e) document.getElementById("userid").innerHTML = "" setTimeout(() => connectWS(), 1000) }