multiple clients

main
alex 2023-02-27 22:51:12 +01:00
parent e384f7c02c
commit 7101c71889
3 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple</title>
<script src="script.js"></script>
</head>
<body>
<ul id="ws-list"></ul>
<style>
body {
background-color: black;
color: white;
}
</style>
</body>
</html>

84
ws-sse/multiple/script.js Normal file
View File

@ -0,0 +1,84 @@
/* DEFINITIONS */
const localhost = true
const sseEnabled = false
const wssEnabled = true
let wsPort = 3000
let wsPort2 = 3001
let wsAddressLocal = "ws://localhost:"
const wsAddressServer = "wss://alpha-ws.clickandjoin.umbach.dev"
/* DEFINITIONS END */
let wsList;
window.onload = () => {
wsList = document.getElementById("ws-list")
if (localhost) {
wsAddress = wsAddressLocal
} else {
wsAddress = wsAddressServer
}
wsClient(wsPort)
wsClient(wsPort2)
wsClient(wsPort)
wsClient(wsPort2)
wsClient(wsPort)
wsClient(wsPort2)
}
let count = 0
function wsClient(wsPort) {
let li = document.createElement("li")
li.id = "client-" + count
li.innerHTML = "Client: " + count + " "
let sp = document.createElement("span")
sp.id = "client-userid-" + count
li.appendChild(sp)
wsList.appendChild(li)
if (localhost) {
wsAddress = wsAddressLocal + wsPort
}
let c = count
ws = new WebSocket(wsAddress + "/?auth=WAZgOGzc-g5VC-zbav-KxCT-bCNlFfQk6ptl")
ws.onopen = () => {
console.info("ws open " + c)
}
ws.onmessage = (msg) => {
console.log("rec msg:", msg.data)
let data = JSON.parse(msg.data)
// Only used here to test whether messages can be sent between the servers
if (data["Cmd"] == 99999) {
document.getElementById("client-userid-"+c).innerHTML = data["Body"]
}
}
ws.onclose = (e) => {
console.log("closed", e.reason.code)
if (e.reason.code === 1005) return
console.log("ws closed", e)
document.getElementById("client-userid-"+c).innerHTML = ""
//setTimeout(() => wsClient(), 100)
}
ws.onerror = (err) => {
console.warn("err:", err)
}
count++
}

View File

@ -115,6 +115,7 @@ function connectWS() {
}
ws.onclose = (e) => {
console.log("closed", e.reason.code)
if (e.reason.code === 1005) return
console.log("ws closed", e)