You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.2 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>websocket</title>
</head>
<body>
<input type="text" id="uname">
<button id="btn">连接</button>
<script type="text/javascript">
document.getElementById("btn").onclick = function() {
const uname = document.getElementById("uname").value
console.log(uname)
connect(uname)
}
function connect(uname) {
const ws = new WebSocket("ws://localhost:9999/ws")
ws.onopen = (e) => {
console.log('open', e)
document.getElementById("uname").value = ""
document.getElementById("btn").innerText = '发送'
document.getElementById("btn").onclick = function () {
const msg = document.getElementById("uname").value
const token = "AgOjcdf3goeYDX3lwWWwkXtVpcrL-l2rX8csrRKgs3_-BC3JOx0l6nZU0MV25eIn"
ws.send(JSON.stringify({t: token}))
document.getElementById("uname").value = ''
}
}
ws.onmessage = (e) => {
console.log('msg:', e.data)
}
ws.onerror = (a,b,c) => {
console.log('error:', a, b, c)
}
}
</script>
</body>
</html>