85 lines
1.8 KiB
JavaScript
85 lines
1.8 KiB
JavaScript
/* 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++
|
|
}
|