github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/transport/internet/websocket/dialer.html (about)

     1  <!DOCTYPE html>
     2  <html>
     3  <head>
     4  	<title>Browser Dialer</title>
     5  </head>
     6  <body>
     7  	<script>
     8  		// Copyright (c) 2021 XRAY. Mozilla Public License 2.0.
     9  		var url = "ws://" + window.location.host + "/websocket"
    10  		var count = 0
    11  		setInterval(check, 1000)
    12  		function check() {
    13  			if (count <= 0) {
    14  				count += 1
    15  				console.log("Prepare", url)
    16  				var ws = new WebSocket(url)
    17  				var wss = undefined
    18  				var first = true
    19  				ws.onmessage = function (event) {
    20  					if (first) {
    21  						first = false
    22  						count -= 1
    23  						var arr = event.data.split(" ")
    24  						console.log("Dial", arr[0], arr[1])
    25  						wss = new WebSocket(arr[0], arr[1])
    26  						var opened = false
    27  						wss.onopen = function (event) {
    28  							opened = true
    29  							ws.send("ok")
    30  						}
    31  						wss.onmessage = function (event) {
    32  							ws.send(event.data)
    33  						}
    34  						wss.onclose = function (event) {
    35  							ws.close()
    36  						}
    37  						wss.onerror = function (event) {
    38  							!opened && ws.send("fail")
    39  							wss.close()
    40  						}
    41  						check()
    42  					} else wss.send(event.data)
    43  				}
    44  				ws.onclose = function (event) {
    45  					if (first) count -= 1
    46  					else wss.close()
    47  				}
    48  				ws.onerror = function (event) {
    49  					ws.close()
    50  				}
    51  			}
    52  		}
    53  	</script>
    54  </body>
    55  </html>