Update dialer.html

This commit is contained in:
RPRX 2021-03-23 16:55:05 +00:00 committed by GitHub
parent d46af8b5d4
commit e0910ab4d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,53 +3,53 @@
<head> <head>
<title>Browser Dialer</title> <title>Browser Dialer</title>
</head> </head>
<body></body> <body>
<script> <script>
// Copyright (c) 2021 XRAY. Mozilla Public License 2.0. // Copyright (c) 2021 XRAY. Mozilla Public License 2.0.
var url = "ws://" + window.location.host + "/websocket" var url = "ws://" + window.location.host + "/websocket"
var count = 0 var count = 0
setInterval(check, 1000) setInterval(check, 1000)
function check() { function check() {
if (count <= 0) { if (count <= 0) {
count += 1 count += 1
console.log("Prepare", url) console.log("Prepare", url)
var ws = new WebSocket(url) var ws = new WebSocket(url)
var wss = undefined var wss = undefined
var first = true var first = true
ws.onmessage = function (event) { ws.onmessage = function (event) {
if (first) { if (first) {
first = false first = false
count -= 1 count -= 1
var arr = event.data.split(" ") var arr = event.data.split(" ")
console.log("Dial", arr[0], arr[1]) console.log("Dial", arr[0], arr[1])
wss = new WebSocket(arr[0], arr[1]) wss = new WebSocket(arr[0], arr[1])
var opened = false var opened = false
wss.onopen = function (event) { wss.onopen = function (event) {
opened = true opened = true
ws.send("ok") ws.send("ok")
} }
wss.onmessage = function (event) { wss.onmessage = function (event) {
ws.send(event.data) ws.send(event.data)
} }
wss.onclose = function (event) { wss.onclose = function (event) {
ws.close() ws.close()
} }
wss.onerror = function (event) { wss.onerror = function (event) {
!opened && ws.send("fail") !opened && ws.send("fail")
wss.close() wss.close()
} }
check() check()
} else wss.send(event.data) } else wss.send(event.data)
} }
ws.onclose = function (event) { ws.onclose = function (event) {
if (first) count -= 1 if (first) count -= 1
else wss.close() else wss.close()
} }
ws.onerror = function (event) { ws.onerror = function (event) {
ws.close() ws.close()
}
} }
} }
} </script>
</script>
</body> </body>
</html> </html>